77) Write a program to input a number and
check whether it is Odious Number or
not? (7…binary…111.no. of ones is 3 and it is odd.so it is odious no.)
import java.util.*;public class Odious_num{public static void main(String args[ ]){Scanner sc = new Scanner (System.in);System.out.println("Enter a number");int b = sc.nextInt();int a = b;int r = 0;String c = "";while(a>=1){if(a%2==0){c=c+"0";}else{c=c+"1";}a=a/2;}int k = c.length();for(int i = k-1;i>=0;i--){if(c.charAt(i)=='1'){r++;}}if(r%2!=0){System.out.println("Entered number is a ODIOUS Number");}else{System.out.println("Entered number is not a ODIOUS Number");}}}
No comments:
Post a Comment
if you have any doubts, Please let me know.
Your Truly
Shashank Porwal