Friday, January 23, 2015

Java Code for Number Pattern 4


In the program the following pattern will be printed.

 
         1
       121
     12321
   1234321
 123454321

The program is shown below :

import java.util.*;
class pattern5
{
public static void main(String args[])
{
Scanner scr=new Scanner(System.in);

System.out.println("Enter the number of rows ");
int n=scr.nextInt();

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

for(int k=1;k<=i;++k)
{
System.out.print(k);
}
for(int l=i-1;l>0;--l)
{
System.out.print(l);
}

System.out.println();
}

}// end of main method
}// end of main class



OUTPUT

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

Enter the number of rows
5

    1
   121
  12321
 1234321
123454321


No comments:

Post a Comment