Program opened below. You can scroll through the algorithm, Java code and output.
Program 16
Drabon Number
Question
Read a positive integer and determine whether the number of 1 bits in its binary representation is even.
Algorithm
Step 1: Start execution.
Step 2: Read the positive integer n.
Step 3: Reject any value smaller than one.
Step 4: Initialize the binary-one counter to zero.
Step 5: Copy the number into a temporary variable.
Step 6: Check whether the temporary value is still positive.
Step 7: Find the remainder after division by two.
Step 8: A remainder of one represents a binary digit 1.
Step 9: Increase the count if the remainder is one.
Step 10: Divide the temporary value by two using integer division.
Step 11: Repeat until the temporary number becomes zero.
Step 12: Check the parity of the accumulated count.
Step 13: If the count is even, report Drabon.
Step 14: Otherwise report Not Drabon.
Step 15: Stop.
Java program
Javaimport java.util.*;
class Drabon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n < 1) {
System.out.println("Invalid number");
return;
}
int ones = 0;
int temp = n;
while (temp > 0) {
if (temp % 2 == 1) {
ones++;
}
temp /= 2;
}
System.out.println(ones % 2 == 0 ? "Drabon" : "Not Drabon");
}
}Sample output
Input: 3
Output: Drabon