Friday, January 23, 2015

Java Code for Alphabet Pattern 3


In the program the following pattern will be printed.
   
      A
     BAB
    CBABC
   DCBABCD
  EDCBABCDE

The program is shown below :

import java.util.*;
class pattern1
{
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();
char ch='A';
char c;
for(int i=1;i<=n;++i)        // FOR LOOP FOR NUMBER OF ROWS
{
c=ch;
for(int j=i;j
{
System.out.print(" ");
}
for(int k=1;k<=i;++k)      // FOR LOOP FOR PRINTING ALPHABETS IN DESCENDING ORDER  
{
System.out.print(c);
--c;
}
c+=2;
for(int l=1;l
{
System.out.print(c);
++c;
}
System.out.println();    // INTRODUCING NEW LINE
++ch;                   //  INCREMENTING VALUE OF CH FOR NEXT ITERATION
}
} // end of main method
} // end of main class
Output :

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

Enter the number of rows.
6

     A
    BAB
   CBABC
  DCBABCD
 EDCBABCDE
FEDCBABCDEF


No comments:

Post a Comment