(Coderbyte) Powers of Two - Solução

Have the function PowersofTwo(num) take the num parameter being passed which will be an integer and return the string true if it's a power of two. If it's not return the string false. For example if the input is 16 then your program should return the string true but if the input is 22 then the output should be the string false.

Solução:

import java.util.*;
import java.io.*;

class Function { 
  String PowersofTwo(int num) {
 
    for (int i = 0; Math.pow(2, i) <= num; i++) {
      if (Math.pow(2, i) == num) {
           return "true";
      }
    }
      
    return "false";
   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.PowersofTwo(s.nextLine()));
  }  
 
}           

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução