write a menu driven program in java to accept a number and check whether it is an evil number or an odious number as per the users choice.
Answers
Answer:
// Java program to check if a number is
// Evil number or Odious Number
class GFG {
// returns number of 1s from the binary number
static int count_one(int n)
{
int c_one = 0;
while (n != 0) {
int rem = n % 10;
// counting 1s
if (rem == 1)
c_one = c_one + 1;
n = n / 10;
}
return c_one;
}
// Check if number is evil or not
static int checkEvil(int n)
{
int i = 0, bin = 0, n_one = 0;
// converting n to binary form
while (n != 0) {
// calculating remainder
int r = n % 2;
// storing the remainders in binary
// form as a number
bin = bin + r * (int)(Math.pow(10, i));
n = n / 2;
}
// Calling the count_one function to count
// and return number of 1s in bin
n_one = count_one(bin);
if (n_one % 2 == 0)
return 1;
else
return 0;
}
// Driver Code
public static void main(String[] args)
{
int i, check, num;
num = 32;
check = checkEvil(num);
if (check == 1)
System.out.println(num + " is Evil Number");
else
System.out.println(num + " is Odious Number");
}
}
By ALOK Malakar
Answer: main method
import java.util.*;
class Hello
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.
in);
int n,r,s=0,digit;
System.out.println("Enter a number");
n=sc.nextInt();
String s=" ";
char digit[]={'0','1'};
while(n>0)
{
r=n%2;
s=digit*r+s;
n=n/2;
}
return s;
}
int countone(String s)
{
int c=0,1=s.lenght();
char ch;
for(int i=0;i<1,i++)
{
int n=ob.countOne(bin);
System.out.println("Number of ones="+n);
if(n%2==0)
System.out.println(n+"is a Evil number");
else
System.out.println(n+"is an odious number");
}
}
Instagram:-@anishkumar8064.
Big Thank you
Explanation: