Wednesday, 22 June 2016

Program to print a Number Pattern Type-21.

Explanation :
If n=6 then output will be
            1
          123
        12345
      1234567
    123456789
1234567891011
    123456789
     1234567
       12345
         123
           1  .

Code:

import java.util.Scanner;
public class Pattern21
{
    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=i;j<n;j++)
            {
                System.out.printf(" ");
            }
            for (k=1;k<(i*2);k++)
            {
                System.out.printf("%d",k);
            }
            System.out.printf("\n");
        }
        for (i=n-1;i>=1;i--)
        {
            for (j=n;j>i;j--)
            {
                System.out.printf(" ");
            }
            for (k=1;k<(i*2);k++)
            {
                System.out.printf("%d",k);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

No comments:

Post a Comment