Wednesday, 23 November 2016

Pascal triangle 1

This program will display the pascal triangle or print star pattern as shown in output. 
System.out.print("....."): are used for display message on screen or console but cursor not move in new line. 
System.out.println("....."): are used for display message on screen or console, cursor move in new line.



PROGRAM
class PascalTriangle {

 public static void main(String args[]) {
  
  for(int i=0; i<4; i++) {
   
   int k;
   for(int j=i; j<=5-i; j++) {
    
    // This loop will show blank space
    System.out.print(" ");
   }
   for(k=0; k<=i; k++) {
    
    // This loop will show left side of triangle
    System.out.print("* ");
   }
   for(int l=0; l<i; l++) {
    
    // This loop will show right side of triangle
    System.out.print("* ");
   }
   System.out.println("");
  }
 }
}
OUTPUT
C:\>javac PascalTriangle.java
C:\>java PascalTriangle

         * 
      * * * 
   * * * * * 
* * * * * * * 

No comments:

Post a Comment