import java.util.Scanner;
public class Main
{
static boolean power_2(int nu)
{
if (nu== 1)
return true;
else if (nu % 2 != 0 ||
nu ==0)
return false;
return power_2(nu/ 2);
}
static int sumOfDigits(int n)
{
return n== 0 ? 0 : n%10 +
sumOfDigits(n/10) ;
}
public static int nextPowerOfTwo ( int n )
{
int v = 1;
while(v<=n)
{
v=v<< 1;
}
return v;
}
public static void main(String[] args) {
int number;
Scanner input = new Scanner(System.in);
System.out.println("Enter number\n");
number = input.nextInt();
if(power_2(number)==true){
System.out.println(number+ " is a power of 2\n");
System.out.printf("Sum of %d digits is: %d\n",number, sumOfDigits(number));
}
else{
System.out.println(number+" is not a power of two\n");
System.out.printf("The next power of two is: "+nextPowerOfTwo(number));
}
}
}
Comments
Leave a comment