Question
Print Smith number in given range only using main method.
(A Smith Number is a composite number whose sum of digits is equal to the sum of digits in its prime factorization)
Enter Lower Range:1
Enter Upper Range:100
Smith numbers between 1 and 100:
4 22 27 58 85 94
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class SmithNumberInGivenRange
{
public static void main()
{
int upperRange=0,lowerRange=0;
int sumd=0,sump=0,i=0,j=0,k=0,temp=0,temp1=0,flag=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Lower Range:");
lowerRange=sc.nextInt();
System.out.print("Enter Upper Range:");
upperRange=sc.nextInt();
System.out.println("Smith numbers between "+lowerRange+" and "+upperRange+":");
for(i=lowerRange;i<=upperRange;i++)
{
temp=i;
while(temp>0)
{
sumd=sumd+(temp%10);
temp=temp/10;
}
temp=i;
for(j=2;j< i;j++)
{
while(temp%j==0)
{
flag=0;
//checking whether number is prime number or not
for(k=2;k< j;k++)
{
if(j%k==0)
{
flag=1;
break;
}
}
//if number is prime then only it is added to sump
if(flag==0)
{
temp1=j;
//Extracting digits if i >9 and then adding extracted digits to sump
while(temp1>0)
{
sump=sump+(temp1%10);
temp1=temp1/10;
}
temp=temp/j;
}
}
}
if(sumd==sump && i>0)
{
System.out.print(i+" ");
}
sumd=0;
sump=0;
}
}
}
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
