Smith No

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Ques: - 1) Write a program to check if a no. is Smith Number or not.

A Smith number is a composite number where the sum of its digits is equal
to the sum of its prime factors

Ex: -
Input: n = 4
Output: Yes
Prime factorization = 2, 2 and 2 + 2 = 4
Therefore, 4 is a smith number

Input: n = 6
Output: No
Prime factorization = 2, 3 and 2 + 3 is
not 6. Therefore, 6 is not a smith number

Input: n = 666
Output: Yes
Prime factorization = 2, 3, 3, 37 and
2 + 3 + 3 + (3 + 7) = 6 + 6 + 6 = 18
Therefore, 666 is a smith number

Algorithm
1) START
2) Input the no. from the user
3) Set sum1 and sum 2 to 0
4) Call function sum using variable sum1
5) Function Sum accepts the variable and stores it in a variable n
6) Initialize s=0 and r=0
7) Repeat steps 8 to 10 till n! = 0
8) Save the remainder of n divided by 10 in variable r
9) Add r in the variable s
10) Divide the number n by 10
11) Return s to main function
12) Value of snow stored in sum1
13) Function primesum is called by variable s2
14) Punction primesum accepts the no. and stores in a variable n
15) Initialize f=2, t1, t=n, d, sum =0
16) Repeat steps from 17 to 24 till t is greater than 1
17) If remainder of t divided by f is 0 repeat steps from 18 to 23 otherwise
perform the step 24
18) Store the value of f in t1
19) Repeat the steps 20 to 22 till t1 is not equal to 0
20) Store the remainder of t1 divided by 10 in variable d
21) Add the value of d in sum
22) Divide t1 by 10
23) Divide t by f
24) Increase f by 1
25) Return sum to the main function
26) Value of sum is stored in s2
27) Now in main function if s1 is equal to s2
28) Print It is a smith no.
29) Otherwise
30) Print It is not a Smith no.
31) STOP

Java Program
import java.util.*;
class smudge
{
int sum(int n)
{
int s=0;
while(n!=0)
{
int r=n%10;
s+=r;
n/=10;
}
return s;
}
boolean checkprime(int n)
{
int c=0;
for(int i =1;i<n;i++)
{
if(n%i == 0)
{
c++;
}
}
if(c==2)
{
return true;
}
else
{
return false;
}
}
int primesum(int n)
{
int f=2;int t1;int t=n;int d,sum=0;
while(t>1)
{
if(t%f == 0)
{
t1=f;
while(t1!=0)
{
d=t1%10;
sum+=d;
t1/=10;
}
t/=f;
}
else
{
f++;
}
}
return sum;
}
public void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the no.");
int n = obj.nextInt();
int s1=sum(n);
int s2=primesum(n);
if(s1==s2)
{
System.out.println("Yes it is a smith no.");
}
else
{
System.out.println("No it is not a smith no.");
}
System.out.println(s1);
System.out.println(s2);
}
}

You might also like