Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class _07_Count_of_Bits_One {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.printf("Please enter integer number %n");
- int num = Integer.parseInt(input.nextLine());
- String binary=Integer.toBinaryString(num);
- char binaryArray[]=new char[binary.length()];
- binaryArray=binary.toCharArray();
- int countOnes=0;
- for (char i:binaryArray)
- {
- if (i=='1')
- {
- countOnes++;
- }
- }
- System.out.printf("The number of ones in binary representation of %d is %d", num, countOnes);
- System.out.printf(binary);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement