Java program for printing the below output


 import java.util.Scanner;  
 public class PracticeDemo {  
      public static void getNumber(int number){  
    int middle=1;  
    String start="1",end="1",output;  
    System.out.println(start);  
    for(int i=1;i<=number;i++){  
         middle=middle*2;  
          output=start+" "+middle+" "+end;  
          System.out.println(output);  
          start=start+" "+middle;  
                 end=middle+" "+end;  
        }  
      }  
      public static void main(String [] args){  
      Scanner sc=new Scanner(System.in);  
      System.out.println("Enter the number");  
      int x=sc.nextInt();  
      getNumber(x);  
      sc.close();  
      }  
      }  


Ouput:

Enter the number
10


1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 1024 512 256 128 64 32 16 8 4 2 1

Comments

Popular posts from this blog

Reasoning-Number Series

Reasoning-Letter Series

Profit and Loss