Smith Number - Coder's point

Saturday, March 16, 2019

Smith Number

 
//A Smith Number is a composite number whose sum of digits is equal to the sum of digits in its prime factorization. import java.util.*; class Smith_number { int Sumdig(int num) { int sum=0; while(num>0) { sum=sum+num%10; num=num/10; } return sum; } int factsum(int num) { int fact=0; int i=2; while(num>1) { if(num%i==0) { fact=fact+Sumdig(i); num=num/i; } else { i++; } } return fact; } public static void main() { Smith_number obj=new Smith_number(); Scanner in = new Scanner(System.in); System.out.println("Enter any number"); int num=in.nextInt(); int a=obj.Sumdig(num); int b=obj.factsum(num); if(a==b) { System.out.println("Smith Number"); } else { System.out.println("Not a Smith Number"); } } }
OUTPUT:

Enter any number
4
Smith Number
Enter any number
6
Not a Smith Number

No comments:

Post a Comment