Wednesday, 22 June 2016

Program to print a Number Pattern Type-8.

Explanation :
If n=6 then output will be
1
12
123
1234
12345
123456
12345
1234
123
12
1.

Code:

import java.util.Scanner;
public class Pattern8
{
    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 ck=0,c=2;
        while(c>0)
        {
            if(ck == 0)
            {
              for(int i = 1;i <= n ; i++)
              {
                for(int j =1 ; j <= i  ; j++)
                {
                  System.out.print(j);
                }
                  System.out.println();
               }
               ck++;     
             }
            else
            {
               for(int i =1 , r = n-1 ; i <= n - 1 ; i++ ,r--)
               {
                  for(int j = 1 ; j <= r; j++)
                  {
                     System.out.print(j);
                  }
                     System.out.println();
                }
             }
               c--;
      }
        sc.close();
    }
}

No comments:

Post a Comment