Java program to print numbers in triangle and pyramid vice


Program 1

package com.candidjava;

import java.util.Scanner;

public class PatternNuberPyramidPrevRev
{

	public static void main(String args[])
	{
		int s = 1;
		int n;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the N values");
		n = sc.nextInt();
		for (int i = 1; i <= n; i++)
		{
			while (s <= i)
			{
				System.out.print(s);
				s++;
			}
			s--;
			while (s > 1)
			{
				System.out.print(--s);

			}
			System.out.println();
		}
	}
}

Output 1

Enter the N values

5

1

121

12321

1234321

123454321


Program 2

package com.candidjava;

import java.util.Scanner;

public class PatternNumberPyramid
{
	public static void main(String args[])
	{

		int i, j, n;

		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the Row value  n");
		n = sc.nextInt();
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= i; j++)
				System.out.print(" " + j);
			System.out.print("\n");

		}

	}

}

Output 2

Enter the Row value  n
5
 1
 1 2
 1 2 3
 1 2 3 4
 1 2 3 4 5

Program 3

package com.candidjava;

public class PatternNumberPyramidUptoN
{

	public static void main(String args[])
	{
		{
			int i, j, n = 1;
			for (i = 0; i < 5; i++)
			{
				for (j = 0; j <= i; j++)
				{
					System.out.print(n + " ");
					n++;
				}
				System.out.println();
			}
		}

	}
}

Output 3
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 




Related Post

Comments


©candidjava.com