Sei sulla pagina 1di 10

# /*Algorithm for main() method :

## Step 1:Start of algotithm

Step 2: Input the number to be checked and store it in integer variable 'n'
Step 3: Create dublicate values of 'n' and store them in integer variables 'd' and 'd1'
Step 4: Create a while loop to get the length of required array to store the
remainders
(a)A loop is created which runs untill value of n becomes 0
(b)The number of times the loop runs is stored in the integer 'count'
Step 5: Create 'array[]' and 'array2[]' of size 'count'
Step 6: Create another while loop to find and store the remainders in 'array[count]'
(a)A loop is created which runs untill value of d becomes 0
(b)the remainders are found and stored in 'array[count]'

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

## Step 9:Create a if conditional to check if there are even number of ones

(a)Check if sum%2==0, if true the increase value of 'flag' by 1
Step10: Display the Binary equivalent and number of 1's
Step 11: Create a if conditional to check if 'flag'>0, if true display "Evil Number"
else display "notEvil number"
Step 12: End of Algorithm

*/
import java.io.*;
public class EvilNumber
{
int n,d,count,val,x,i,y,k,sum,flag,d1,l;
public void main() throws IOException
{
System.out.println("Enter number to checked:");
d=n;
d1=d;

while(n>0)

## //while loop to calculate the required length of array

{
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++;

## //while loop to find and store the remainders in

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++;
}

{
if(array2[k]==1)
sum++;
}
if(sum%2==0)

## //if conditional to check if there are even number of ones

{
flag++;
}
System.out.print("INPUT:"+ d1);
number of 1's

## // Displaying the Binary equivalent and

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");
}
}
}

## /*Algorithm for main() method :

Step 1:Start of algotithm
Step 2: Input the number to be checked and store it in integer variable 'n'
Step 3: Check if 'n' is Prime with the help of a for loop
(a)The loop runs from 2 till 'n' and checks if 'n' is divisible by any of its
preceeding numbers
using the modulus operator
Step 4:If 'n' is Prime, intger variable 'rev' gets the value returned by the function
'reverse(n)' else,
'rev'=0
Step 5:Display "yes" if the value of 'rev'==1 else display "No"

## Step 1:Create a Dublicate integer value of 'n' and store it in'x'

Step 2:Create a while loop, reverse the inputed integer value and store it in 'sum'
(a)The loop runs untill 'n'>0
'digit'='n'%10;
'sum'='sum'*10+'digit';
'n'='n'/10;
Step 3:Create a for loop and check if the reversed number is Prime
(a)The loop runs from 2 till 'sum' and checks if 'sum' is divisible by any of its
preceeding numbers
using the modulus operator
Step 4:Return 1 if 'sum' is Prime else return 0;
Step 5: End of Algorithm
*/
public class ReversePrime
{
int rev;
public void main(int n)
{
int flag=0;
for(int c=2;c<n;c++)
{
if(n%c==0)
{
flag++;
}
}
if(flag==0)

rev=reverse(n);

## //calling function reverse(n)

else
rev=0;

if(rev==1)
System.out.println("Yes");
else
System.out.println("No");
}

## //reversing inputed value and checking if Prime

{
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;

}
}

## /* Algorithm for main() method

* Step1: Start of algorithm
* Step 2: Input the begining limit and the end limit and store them in 'p' and 'q'
respectively
* Step 3:Create an if conditional to check if 'p' or 'q' exceeds 5000, if yes display
"out of range"
*

## * Algorithm for calculate() method

* Step 1: Create a outer while loop which runs till end limit and checks if a number
is Kaprekar
* Step 2: Create an inner while loop which runs till'p'!=0 and checks the length of
the integer and
*

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;

## * Step 4: if 'sum'=='num' print the number and add 1 to 'count'

* Step 5: Increase the value of 'num' by 1 and if it is less then 'q' (end limit) of outer
while
*

## * Step 6: Display frequency by printing 'count'

* Step 7: End of Algorithm
*/

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);

}
}