Gadgets

Showing posts with label triangle program. Show all posts
Showing posts with label triangle program. Show all posts

Wednesday, 9 December 2015

Print numbers in triangle format

Print numbers in triangle format
public class TriangleTest {
    public static void main(String[] arg){
        int a=1;
        for(int i=1;i<=4;i++){
            for(int space=i;space<4;space++){
            System.out.print(" ");   // for print spaces
            }
            for(int j=1;j<=i;j++){
                System.out.print(a+" ");//print number
                a++;
            }
            System.out.println();// for new line
        }
    }
/*
Output:
   1
  2 3
 4 5 6
7 8 9 10
*/













Tuesday, 24 November 2015

Print numbers in triangle format

 
Print numbers in right triangle format 
public class Triangle {
    public static void main(String[] arg){
        for(int i=1;i<=5;i++){  // loop for number of rows
            for(int j=1;j<=i;j++){
                System.out.print(j); // loop for print numbers
            }
            System.out.println();// new line
        }
    }    
} 
Output:
1
12
123
1234
12345
 Print numbers in left triangle format 
  public class Triangle {
     public static void main(String[] arg){
        for(int i=0;i<=6;i++){   // loop for number of rows     
            for(int space=i;space<6;space++){  // loop for spaces for each row
                System.out.print(" ");
            }                   
            for(int j=0;j<i;j++){   // loop for print stars (*)
                System.out.print("*");
            }
            System.out.println("");// new line
            }       
      }    
Output:
                
     *
    **
   ***
  ****
 *****
******
 
Print numbers in triangle format 
public class Triangle {
       public static void main(String[] arg){
        for(int i=0;i<=6;i++)  // loop for number of rows
                                              {            
            for(int space=i;space<6;space++){ // loop for spaces for each row
                System.out.print(" ");
            }                   
            for(int j=0;j<i*2+1;j++){ 
 
            for(int j=0;j<i;j++){  // loop for print stars (*)
 
                System.out.print("*");
            }
            System.out.println("");// new line
            }          
        }    
}
 
Output:
      *
     ***
    *****
   *******
  *********
 ***********
*************