Programming Examples

Java program to input a number and check whether it is a Smith number or not


Write a program to input a number and check whether it is a Smith number or not. Smith number is such a number, the sum of whose digits equals the sum of the digits of its prime factors.

Smith number is a composite number in which the sum of its digits is equal to the sum of the digits of all its prime factors.

For Example 378 is a Smith Number as the sum of the digits of 378 are : 3+7+8 = 18. The prime factors of 378 are: 2, 3, 3, 3, 7 ( sum = 2+3+3+3+7 = 18).

Similarly 22 is a Smith Number as the sum of the digits are : 2+2=4. The prime factors of 22 are:

2 and 11 (Sum = 2+(1+1) = 4

Other Examples include 27, 58, 85, 94, 121, 166, 202, 265, etc.

Solution

import java.util.*;
class SmithNumber
{
	public static void main(string arr[])
	{
		Scanner sc=new Scanner(System.in);
		int n,f=2,m,t,s1=0,s2=0,d;
		System.out.println(“Enter a number:”);
		n=sc.nextInt();
		m=n;
		while(n>1)
		{
			if(n%f==0)
			{
				t=f;
				while(t!=0)
				{
					d=t%10;
					s1+=d;
					t/=10;
				}
				n=n/f;
			}
			else
				f++;
		}
		t=m;
		while(t!=0)
		{
			d=t%10;
			s2+=d;
			t/=10;
		}
		if(s1==s2)
			System.out.println(“Smith Number”);
		else
			System.out.println(“Not a Smith Number”);
	}
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz