Sunday, 19 June 2016

Program to print a Number Pattern Type-1.

Explanation :
If n=6 then output will be
66666666666
65555555556
65444444456
65433333456
65432223456
65432123456
65432223456
65433333456
65444444456
65555555556
66666666666.

Code:

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

No comments:

Post a Comment