Friday, January 23, 2015

Java Code for Number Pattern 2


In the program the following pattern will be printed.


          1
        21
      321
    4321
  54321


The program is shown below :

import java.util.*;
class pattern9
{
public static void main(String args[])
{
Scanner scr=new Scanner(System.in);
int n;
System.out.println("Enter the number of rows");
n=scr.nextInt();

for(int i=1;i<=n;++i)
{
for(int k=n;k>i;--k)
System.out.print(" ");

for(int j=i;j>=1;--j)
{
System.out.print(j);
}
System.out.println();
}
}
}

OUTPUT

C:\Users\Ravi\Desktop\ravi>javac pattern9.java
C:\Users\Ravi\Desktop\ravi>java pattern9

Enter the number of rows
6

          1
        21
      321
    4321
  54321
654321


No comments:

Post a Comment