Home / JAVA / ICSE Class 10 / Program for smith number in Java

Program for smith number in Java

This is a program in java to check whether a number is a smith number or not.

import java.io.*;
class smithnumber
{ 
	static int sumdigit(int x)  //to find the sum of digits
	{
        int sum=0;
	if(x!=0)
	sum+=x%10+sumdigit(x/10);
	return sum;
	}

	static int sumfact(int x)
	{
		int i,fact=0;
		for(i=2;i<=x;i++)
		{	if(prime(i))
			{ 	while(x%i==0)
				{	fact+=sumdigit(i);
					x/=i;
					}
			}
		}
	return fact;
	}

	static boolean prime(int x)
	{	int i,count=0;
		for(i=2;i<x;i++)
		{	if(x%i==0)
			count++;
		}
		if(count==0)
		return true;
		else
		return false;
	}
	public static void main(String args[])throws IOException
	{	int a,sdig,sfact;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("enter a number to check if it is a smith number or not");
		a=Integer.parseInt(br.readLine());
		sdig=sumdigit(a);
		sfact=sumfact(a);
		if(sdig==sfact)
		System.out.println("The number "+a+" is a Smith number");
		else
		System.out.println("Not a smith number");
	}		
}

OUTPUT:

Java program for smith number

Tags: Program for smith number in java, java program for smith number.

Also See:   Java Program to check for a Kaprekar number

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

Your email address will not be published. Required fields are marked *