0% found this document useful (0 votes)
134 views2 pages

Smith Number PDF

This Java program finds and prints all Smith numbers within a given range of integers. The user inputs a starting and ending index. It uses helper methods to check if a number is prime and to calculate the sum of its digits. For each number in the range, it factors the number into prime factors, calculates the sum of each factor and compares it to the original number's digit sum to determine if it is a Smith number.

Uploaded by

FAISAL GHEYAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views2 pages

Smith Number PDF

This Java program finds and prints all Smith numbers within a given range of integers. The user inputs a starting and ending index. It uses helper methods to check if a number is prime and to calculate the sum of its digits. For each number in the range, it factors the number into prime factors, calculates the sum of each factor and compares it to the original number's digit sum to determine if it is a Smith number.

Uploaded by

FAISAL GHEYAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SMITH NUMBER

import java.util.*;
class Smith
{
static int prime(int a)
{
int c=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
{
c++;
}
}
if(c==0)
return 1;
else
return 0;
}
static int sum(int a)
{
int s=0;
while(a>0)
{
int d=a%10;
s=s+d;
a=a/10;
}
return s;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter starting index and ending index");
int start=sc.nextInt();
int end=sc.nextInt();
if(start<=end)
{
System.out.println("all smith numbers in range "+start+" to "+end);
for(int a=start;a<=end;a++)
{
int n=a;
int s1=sum(n);
int s2=0;
int i=2;
while(n>1)
{
int k=prime(i);
if(k==1)
{
while(n%i==0)
{
s2=s2+sum(i);
n=n/i;
}
}
i++;
}
if(s1==s2)
System.out.println(a);
}
}
else
System.out.println("Wrong range");
}
}

You might also like