1. Program to check whether a number is Smith Number or not.
A smith number is a composite number, the sum of whose digits is the sum of
the digits of its prime factors obtained as a result of prime factorisation
(excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 …
Example:
666
Prime factors are 2, 3, 3 and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7) = 18
Sample input:666
Sample output:Smith Number
Algorithm
Algorithm for sumDig
1. Start
2. Create a variable int s
3. Repeat steps 4 and 5 while n is greater than 0
4. Add n%10 to s
5. Divide n by 10 and store it in n
6. Return s
7. Stop
Algorithm for sumPrimeFact
1. Start
2. Create two int variables i=2 and sum=0
3. Repeat from step 4 to step 7 while n>1
4. If n%i==0 then perform steps 5 and 6 and then go to 8 else perform step 7
5. Pass i to sumDig and add it to sum
6. Divide n by i and store it in n
7. Add 1 to i
8. Return sum
9. Stop
Algorithm for main
1. Start
2. Create an object ob of classs smith
3. Create an object in of Scanner Class
4. Print message to enter a number and take the input in a variable int n
5. Pass n to sumDig of ob an d store it in int a
6. Pass b to sumPrimeFact of ob and store it in int b
7. If a==b then print it is a smith number else print it is not a smith number
8. Stop
import java.util.*;
class Smith
{
int sumDig(int n)//Sum of Digits
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
int sumPrimeFact(int n)//Sum of primes
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i); //Finding Sum
n=n/i;
}
else
i++;
}
return sum;
}
public static void main(String args[])
{
Smith ob=new Smith();
Scanner in=new Scanner(System.in);
System.out.println("Enter a Number");
int n=in.nextInt();
int a=ob.sumDig(n);// finding sum of digit
int b=ob.sumPrimeFact(n); //finding sum of prime factors
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}
}
VARIABLE DESCRIPTION
NAME TYPE USE
n Int To take input
i Int To run loop
s Int To find sum
2.Program to check whether a number is an evil number or not
An Evil number is a positive whole number which has even number of 1’s in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1’s.
A few evil numbers are 3, 5, 6, 9….
Sample input:9
Sample Output:Evil number
Algorithm
Algorithm for Binary
1. Start
2. Declare two variables int r and string s
3. Create a character array dig with characters 0 and 1
4. While n>0 repeat steps 4,5 and 6
5. Store n%2 in r
6. Pass r as index to dig and add it to s
7. Divide n by 2 and store it in n
8. Return s
9. Stop
Algorithm for count
1. Start
2. Create two int variables c=0 and l=s.length()
3. Create a char variable ch
4. Int i=0 and repeat steps 4,5 and 6 while i<l
5. Store character at index i of s in ch
6. If ch==’1’ then add one to c
7. Add one to i
8. Return c
9. Stop
Algorithm for main
1. Start
2. Create an object ob of class EvilNumber
3. Create an object in of Scanner class
4. Display message to enter a number and take the input in int n
5. Pass n to Binary of ob and store it in String st
6. Pass st to count of ob and store it in int x
7. Check if x is divisible by x or not. If divisible then print Evil Number else
print not an evil number
8. Stop
import java.util.*;
class EvilNumber
{
String Binary(int n) // To convert decimal to binary
{
int r;
String s="";
char dig[]={'0','1'}; //array to find binary equivalent
while(n>0)
{
r=n%2;
s=dig[r]+s;
n=n/2;
}
return s;
}
int count(String s) // To count the number of ones
{
int c = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i);
if(ch=='1')
{
c++;
}
}
return c;
}
public static void main(String args[])
{
EvilNumber ob = new EvilNumber();
Scanner in = new Scanner(System.in);
System.out.println("Enter a positive number");
int n = in.nextInt();
String st=ob.Binary(n);
int x=ob.count(st);
if(x%2==0)//To check evil Number
System.out.println(n+" is an Evil Number.");
else
System.out.println(n+" is Not an Evil Number.");
}
}
VARIABLE DESCRIPTION
NAME TYPE USE
n Int To take input
i Int To run loop
s Int To find sum
3.Program to check whether a number is a kaprikar number or not
A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a
right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.
If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few
Kaprekar numbers are: 9, 45, 297 ……..
Example 1: 9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Sample input:45
Sample output:Kaprikar number
Algorithm
Algorithm for input
1. Start
2. Create an object in of Scanner class
3. Dispaly message to enter a number and take the input in n
4. Stop
Algorithm for check
1. Start
2. Declare an int variable sq =n x n, l=s.length() and k
3. If l%2==0 then split sq in two equal parts.Add them and store it in k
4. Else divide sq into unequal parts and store it in k
5. If k==n then return true else return false
6. Stop
Algorithm for main
1. Start
2. Create an object ob of class kaprikar
3. Call input of ob
4. Call check of ob and pass it to if statement
5. If check gives true then print kaprikar number else print not a kaprekar
number
6. Stop
import java.util.*;
class kaprikar
{
int n;
kaprikar()//Default Constructor
{
n=0;
}
void input()//To take input
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number");
n=in.nextInt();
}
boolean check()//To check kaprikar
{
int sq=n*n;//Squaring Number
String s=Integer.toString(sq);
int l=s.length();
int k;
if(l%2==0)
{
k=(sq%((int)Math.pow(10,l/2)))+sq/((int)Math.pow(10,l/2));//Splitting and adding
}
else
{
k=(sq%((int)Math.pow(10,(l/2)+1)))+(sq%((int)Math.pow(10,(l/2))));
}
if(k==n)//Checkin Kaprikar
return(true);
else
return(false);
}
public static void main(String Args[])
{
kaprikar ob=new kaprikar();
ob.input();
if(ob.check())
System.out.println("Kaprekar Number");
else
System.out.println("Not a Kaprekar Number");
}
}
VARIABLE DESCRIPTION
NAME TYPE USE
n Int To take input
sq Int To store square
K Int To divde the number into two
parts
4. Program to check whether a number is a bouncy number or not
Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it is
called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a
decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a
“bouncy” number; for example, 155349. Clearly there cannot be any bouncy numbers below 100.
Sample input:15349
Sample ouput:Bouncy number
Algorithm
Algorithm for isIncreasing
1. Start
2. Convert n to string and store it in a String variable s
3. Declare a char variable ch and int variable f=0
4. Int i=0 and repeat step 4 and 5 while i<s.length()-1
5. Find character at index i in s and store it in ch
6. If ch is greater than the value of character at i+1 then store the value of f as 1 and
break
7. If f==1 then return false else return true
8. Stop
Algorithm for isDecreasing
1. Start
2. Convert n to string and store it in a String variable s
3. Declare a char variable ch and int variable f=0
4. Int i=0 and repeat step 4 and 5 while i<s.length()-1
5. Find character at index i in s and store it in ch
6. If ch is less than the value of character at i+1 then store the value of f as 1 and break
7. If f==1 then return false else return true
8. Stop
Algorithm for isBouncy
1. Start
2. Pass n to isIncreasing.If it returns true then print Increasing number
3. Pass n to isDecreasing.If it returns true then print Decreasing number
4. If neither of 2 and 3 are executed then print that it is a bouncy number
5. Stop
Algorithm for main
1. Start
2. Create an object ob of class BouncyNumber
3. Create an object in of Scanner class
4. Display message to input a number and store the input in a variable int n
5. Call isBouncy of ob by passing n
6. Stop
class BouncyNumber
{
boolean isIncreasing(int n) //To check increasing
{
String s = Integer.toString(n);
char ch;
int f = 0;
for(int i=0; i<s.length()-1; i++)
{
ch = s.charAt(i);
if(ch>s.charAt(i+1))
{
f = 1;
break;
}
}
if(f==1)
return false;
else
return true;
}
boolean isDecreasing(int n) //To Check Decreasing
{
String s = Integer.toString(n);
char ch;
int f = 0;
for(int i=0; i<s.length()-1; i++)
{
ch = s.charAt(i);
if(ch<s.charAt(i+1))
f = 1;
break;
}
if(f==1)
return false;
else
return true;
}
void isBouncy(int n)
{
if(isIncreasing(n))
System.out.println("Increasing Number");
else if(isDecreasing(n))
System.out.println("Decreasing Number");
else
System.out.println("Bouncy Number");
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
BouncyNumber ob = new BouncyNumber();
System.out.println("Enter a number");
int n = in.nextInt();
ob.isBouncy(n);
}
}
VARIABLE DESCRIPTION
NAME TYPE USE
n Int To take input
ch Char To extract character
f Int To find increasing or decreasing
5. Write a program to input a number and check whether it is a fascinating number or
not.
Fascinating Numbers: Some numbers of 3 digits or more exhibit a very interesting property.
The property is such that, when the number is multiplied by 2 and 3, and both these products are
concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless
of the number of zeroes.
For eg;192
Consider the number 192,
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results : 192384576
It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once. Hence, it could
be concluded that 192 is a Fascinating Number.
Sample input: (i)219
(ii)123
Sample output: (i)Fascinating number
(ii)Not a fascinating Number
ALGORITHM
Algorithm for Unique()
1. Start
2. Create an array A[ ] to store the digits
3. Create integer variables i and flag
4. Repeat step 5 to 7 till i<q.length()
5. Extract character from I index in string q
6. Convert the ascii value of the character into numeric value by subtracting 48 and store it
in array A[ ]
7. Add 1 to i
8. Repeat steps 9 for 10 times
9. Check if frequency is 1.If not equal to 1, then go to step 10
10. Make the value of flag 1
11. Check if flag is 1.If 1,then return false else return true.
12. Stop
Algorithm for main()
1. Create an object in of Scanner Class
2. Create an object ob of class
3. Display suitable message and take input in variable n
4. Convert the number to string and store it in p
5. Check if the length of p is less than 3. If less,then print that the number must be greater
than 99 and go to 8
6. Add the string converts of the number,numberx2 and numberx3 and store it in string
variable s.
7. Perform unique() for s and give the returned value to if statement.If true then print
Fascinating number else not fascinating.
8. Stop
import java.util.*;
class FascinatingNumber
{
boolean Unique(String q)
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit from '0' to '9'
int i, flag = 0;
char ch;
for(i=0; i<q.length(); i++)
{
ch = q.charAt(i);
A[ch-48]++;
}
for(i=1; i<10; i++)
{
//checking if every digit from '1' to '9' is present exactly once or not
if (A[i]!=1)
{
flag = 1; //flag is set to 1 if frequency is not 1
break;
}
}
if(flag == 1)
return false;
else
return true;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
System.out.println("Enter a number");
int n = in.nextInt();
String p = Integer.toString(n); //converting the number to String
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
else
{
String s = Integer.toString(n*1) + Integer.toString(n*2) + Integer.toString(n*3);
/* Joining the first, second and third multiple of the number
* by converting them to Strings and concatenating them*/
if(ob.Unique(s))
System.out.println(n+" is a Fascinating Number.");
else
System.out.println(n+" is not a Fascinating Number.");
}
}
}
VARIABLE DESCRIPTION
NAME TYPE USE
n Int To take input
i Int To run loop
ch char To extract character