Advertisement
Guest User

Untitled

a guest
May 12th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class _07_Count_of_Bits_One {
  5.  
  6. public static void main(String[] args) {
  7. Scanner input = new Scanner(System.in);
  8. System.out.printf("Please enter integer number %n");
  9. int num = Integer.parseInt(input.nextLine());
  10. String binary=Integer.toBinaryString(num);
  11. char binaryArray[]=new char[binary.length()];
  12. binaryArray=binary.toCharArray();
  13. int countOnes=0;
  14. for (char i:binaryArray)
  15. {
  16. if (i=='1')
  17. {
  18. countOnes++;
  19. }
  20. }
  21. System.out.printf("The number of ones in binary representation of %d is %d", num, countOnes);
  22. System.out.printf(binary);
  23. }
  24.  
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement