Home > DeveloperSection > Blogs > Smith numbers in java

#### Smith numbers in java

Ratings:
0 Comment(s)
1991  View(s)
Rate this:

Smith numbers in java

In this blog I am provide you the code Smith numbers in java.

import java.util.Scanner;

import java.io.*;

public class smith

{

public void findNumber()

{

int num,n,x,y,sumdigits=0,sumfactors=0;

boolean flag;

Scanner scan=new Scanner(System.in);

System.out.print("Enter the number:");

n=scan.nextInt();

num=n;

sumdigits=sumOfDigits(n);

for(x=2;x<=n;x++)

{

if(n%x==0)

{

flag=isPrime(x);

if(flag)

{

y=x;

while(y>0)

{

sumfactors=sumfactors+y%10;

y=y/10;

}

n=n/x;

x--;

}

}

}

if(sumdigits==sumfactors)

System.out.println("\n"+num+ " is a smith number.");

else

System.out.println("\n"+num+ " is not a smith number.");

}

//to print digits of n and return their sum

public int sumOfDigits(int n)

{

int rem, s=0;

while(n>0)

{

rem=n%10;

/*System.out.print(rem+" ");*/

s=s+rem;

n=n/10;

}

return s;

}

//to check i isprime or not

public boolean isPrime(int n)

{

int x;

boolean flag=true;

for(x=2;x< n;x++)

{

if(n%x==0)

{

flag=false;

break;

}//if ends

}//for ends

return flag;

}//isPrime ends

//to call other methods

public static void main(String args[])

{

smith obj=new smith();

obj.findNumber();

}//main() ends

}//class ends

## Output: -

Don't want to miss updates? Please click the below button!

Follow MindStick