Wednesday, 22 June 2016

Program to print a Number Pattern Type-22.

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

Code:

import java.util.Scanner;
public class Pattern22
{
    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 k=1;
         for(int i = 1 ; i <= n ; i++)
         {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(" "+k++);
             }
            System.out.println();
        }
        sc.close();
    }
}

No comments:

Post a Comment