Quantcast
Channel: How to check if a number is a power of 2 - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Answer by Marcus Moo for How to check if a number is a power of 2

$
0
0

This is another method to do it as well

package javacore;import java.util.Scanner;public class Main_exercise5 {    public static void main(String[] args) {        // Local Declaration        boolean ispoweroftwo = false;        int n;        Scanner input = new Scanner (System.in);        System.out.println("Enter a number");        n = input.nextInt();        ispoweroftwo = checkNumber(n);        System.out.println(ispoweroftwo);    }    public static boolean checkNumber(int n) {        // Function declaration        boolean ispoweroftwo= false;        // if not divisible by 2, means isnotpoweroftwo        if(n%2!=0){            ispoweroftwo=false;            return ispoweroftwo;        }        else {            for(int power=1; power>0; power=power<<1) {                if (power==n) {                    return true;                }                else if (power>n) {                    return false;                }            }        }        return ispoweroftwo;    }}

Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>