Odious Number in JavaIn this section, we will learn about finding the odious number in Java. In this section, we will learn what is Odious number and also create Java programs to check if the given number is an Odious number or not. The Odious number program frequently asked in Java coding interviews and academics. Odious NumberThe odious number (On) represents those numbers whose sum of digits in their binary representation is odd. Since a binary representation contains only two digits: 0 and 1. Therefore, we can also say the numbers that contain odd numbers of 1's in their binary representation are called odious numbers. Example of Odious Number1 is an odious number. Because the binary representation of 1 is 1, and 1 is an odd number. 2 is an odious number. Because the binary representation of 2 is 10, and 1 + 0 = 1, and 1 is an odd number. 3 is not an odious number. Because the binary representation of 3 is 11, and 1 + 1 = 2, and 2 is not an odd number. Similarly, we can also check for other number also. AlgorithmStep 1: Take a number n. Step 2: Find the binary representation of the number n. Step 3: Find the sum of all the digits present in the binary representation computed in step 4. Step 5: Check whether the num is odd or not. If the num is odd, then n is the odious number; otherwise, not. Let's implement the above algorithm in a Java program. ImplementationThe following is the implementation to check for the odious numbers using the steps defined above. FileName: OdiousNumberExample.java Output: 1 is an odious number. 2 is an odious number. 3 is not an odious number. 4 is an odious number. 5 is not an odious number. 6 is not an odious number. 7 is an odious number. 8 is an odious number. 9 is not an odious number. 10 is not an odious number. 11 is an odious number. 12 is not an odious number. 13 is an odious number. 14 is an odious number. 15 is not an odious number. 16 is an odious number. 17 is not an odious number. 18 is not an odious number. 19 is an odious number. 20 is not an odious number. Using ArrayListAn array list can also be used to find the odious numbers. AlgorithmStep 1: Take a number n. Step 2: Find the binary representation of the number n. Step 3: Store all the 1's present in the binary representation in an array list. Step 4: Compute the size of the array list. Step 5: Check whether the size of the array list is odd or not. If the size is odd, then n is the odious number; otherwise, not. ImplementationThe following is the implementation to check for the odious numbers using the steps defined above. FileName: OdiousNumberExample1.java Output: 1 is an odious number. 2 is an odious number. 3 is not an odious number. 4 is an odious number. 5 is not an odious number. 6 is not an odious number. 7 is an odious number. 8 is an odious number. 9 is not an odious number. 10 is not an odious number. 11 is an odious number. 12 is not an odious number. 13 is an odious number. 14 is an odious number. 15 is not an odious number. 16 is an odious number. 17 is not an odious number. 18 is not an odious number. 19 is an odious number. 20 is not an odious number.
Next TopicRat in a Maze Problem in Java
|