Wednesday, 22 June 2016

Program to print a Number Pattern Type-20.

Explanation :
If n=6 then output will be
                         1  
                     1  2  3
                1  2  3  4  5
            1  2  3  4  5  6  7
        1  2  3  4  5  6  7  8  9
 1  2  3  4  5  6  7  8  9  10  11  .

Code:

import java.util.Scanner;
public class Pattern20
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1; i<=n; i++)
        {
            for (j=1; j<=n-i; j++)
            {
                System.out.printf("   ");
            }
            for (k=1; k<=2*i-1; k++)
            {
                System.out.printf(" %d ",k);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

No comments:

Post a Comment