Beruflich Dokumente
Kultur Dokumente
Step 7: Create a for loop to reverse 'array[count]' and find the binary equivalent
(a)A loop is created which runs backwards form 'count'-1 to 0
(b)The integers in 'array[count]' are stored in backward order in 'array2[count]'
Step 8: Create a for loop to count the number of 1's
(a)A loop is created which runs from 0 to 'count'
(b)Check if the integer in the present index of 'array2[count]' ==1,
and increase value of sum by 1 if true
*/
import java.io.*;
public class EvilNumber
{
int n,d,count,val,x,i,y,k,sum,flag,d1,l;
public void main() throws IOException
{
BufferedReader obj=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter number to checked:");
n=Integer.parseInt(obj.readLine());
d=n;
d1=d;
while(n>0)
{
n=n/2;
count++;
}
int array[]=new int[count];
int array2[]=new int[count];
x=0;
while(d>0)
'array[count]'
{
val=d%2;
array[x]=val;
x++;
d=d/2;
}
for(i=count-1; i>=0;i--) //for loop to reverse 'array[count]' and find the binary
equivalent
{
array2[y]=array[i];
y++;
}
{
flag++;
}
System.out.print("INPUT:"+ d1);
number of 1's
System.out.println();
System.out.print("BINARY EQUIVALENT:");
for(l=0; l<count;l++)
{
System.out.print(array2[l]);
}
System.out.println();
System.out.print("NO. OF 1's :" + sum);
System.out.println();
if(flag>0) //if conditional to check if there are even number of ones or not
{
System.out.print("EVIL NUMBER"); // Displaying output
}
else
{
System.out.print("NOT AN EVIL NUMBER");
}
}
}
rev=reverse(n);
else
rev=0;
if(rev==1)
System.out.println("Yes");
else
System.out.println("No");
}
{
int n=x;
int flag=0;
int digit,sum=0;
while(n>0)
{
digit=n%10;
sum=sum*10+digit;
n=n/10;
}
for(int c=2;c<sum;c++)
{
if(sum%c==0)
{
flag++;
}
}
if(flag==0)
return 1;
//returning 1 if Prime
else
return 0;
}
}
stores it in 'freq'
*Step 3: Find the square of the number and separate the number in half and add
them
*
num1=num*num;
power=Math.pow(10,freq);
int power1=(int)power;
part1=num1%power1;
part2=num1/power1;
sum=part1+part2;
import java.util.*;
public class Kaprekar
{
int n,p,q,i,count,num,num1,freq,power1,part1,part2,sum;
double power;
public void main()
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter begining limit");
p=scan.nextInt();
System.out.println("Enter end limit");
q=scan.nextInt();
if(p>5000||q>5000)
{
System.out.println("out of range");
}
else
{
calculate();
}
}
void calculate()
{
num=p;
while(num<=q)
{
p=num;
freq=0;
while(p!=0)
{
p=p/10;
freq++;
}
num1=num*num;
power=Math.pow(10,freq);
int power1=(int)power;
part1=num1%power1;
part2=num1/power1;
sum=part1+part2;
if(sum==num)
{
count++;
System.out.println(num+" ");
}
num++;
}
System.out.println("Frequency : " +count);
}
}