Power of 2

 Power of 2

Problem Description


Write a program that takes a command-line argument n and prints a table of the powers of 2 that are less than or equal to


2^n.0 <= n < 31 if u declare as int


Solution;


import java.util.Scanner;

import java.io.*;

public class TestClass {

public static void main(String[] args) { 

Scanner sc= new Scanner(System.in); 

       int a= sc.nextInt();

       int p=1;

   for(int i = 0; i <= a; i++)

       {

     System.out.println(i + " " + p);   

            p = 2 * p;        

       }

     }

}

Post a Comment

0 Comments