Answer to Question #233649 in Java | JSP | JSF for Reddy

Question #233649

Your little brother has a math assignment to find whether the given u power of 2. If it is a power of 2 then he has to find the sum of the digit power of 2, then he has to find the next number which is a power of your help to validate his work. But you are already busy playing video games Develop a program so that your brother can validate his work by



1
Expert's answer
2021-09-06T00:17:05-0400
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int u = in.nextInt();
        int one = 0;
        long sum = 0;
        String bits = Integer.toBinaryString(u);

        for (int i = 0; i < bits.length(); i++) {
            if (bits.charAt(i) == '1') {
                one++;
            }
        }
        if (one == 1) {
            System.out.println(u + " is power of 2");
            do {
                sum += (Math.pow(2, u % 10));
                u /= 10;
            } while (u != 0);
            System.out.println("Sum of digits power of 2 = " + sum);
            System.out.println(Integer.parseInt(bits + '0', 2));
        } else {
            System.out.println(u + " is not power of 2");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS