Wednesday, 22 June 2016

Program to print a Number Pattern Type-12.

Explanation :
If n=6 then output will be
1                    1
12                21
123            321
1234        4321
12345    54321
123456654321.

Code:

import java.util.Scanner;
public class Pattern12
{
    public static void main( String arg[])
    {
        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;j++) {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            for (j=n;j>=1;j--)
            {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

No comments:

Post a Comment