/*This program checks that whether a given number is an Smith Number or not
@auther : Nitendra Kumar
*/
import java.util.Scanner;
public class clSmithNumber
{
public static void main()
{
Scanner in=new Scanner(System.in);
clSmithNumber ob=new clSmithNumber();
int num;
System.out.println("Enter a number");
num=in.nextInt();
boolean bool=ob.fnCheckPrime(num);
if(bool==false)
{
System.out.println("Number is composite");
int sumdigits=ob.fnSumDigits(num);
System.out.println("Sum of digits is:"+sumdigits);
int sumfact=ob.fnSumFact(num);
System.out.println("Sum of prime facors is:"+sumfact);
if(sumdigits==sumfact)
System.out.println("Number is Smith Number");
else
System.out.println("Number is not Smith Number");
}
else
System.out.println("Number is prime so it is not a Smith Number");
}
public boolean fnCheckPrime(int num) //checks whether the number is prime or not
{
int flag=0;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
return true;
else
return false;
}
public int fnSumDigits(int num) //find out sum of digits of number
{
int n1,r1,r=0,sum=0;
do
{
n1=num/10;
r1=num%10;
r+=r1;
num=n1;
}
while(n1>=10);
sum=r+n1;
return sum;
}
public int fnSumFact(int num) //find out sum of prime factors of number
{
int num1,n,sum1=0,sum2=0,sum=0,r=0;
System.out.print("Prime factors are:");
for(int i=2;i<=num;i++)
{
if(num%i==0)
{
System.out.print(i +", ");
num=num/i;
if(i<=10)
sum1+=i;
i--;
if(i>10)
{
i++;
num1=i;
do
{
n=num1/10;
r+=num1%10;
num1=n;
if(num1<10)
{
System.out.println();
sum2=r+num1;
break;
}
}
while(num1>10);
}
}
}
sum=sum1+sum2;
return sum;
}
}
/*------------------Program developed by Nitendra Kumar-----------------*/
@auther : Nitendra Kumar
*/
import java.util.Scanner;
public class clSmithNumber
{
public static void main()
{
Scanner in=new Scanner(System.in);
clSmithNumber ob=new clSmithNumber();
int num;
System.out.println("Enter a number");
num=in.nextInt();
boolean bool=ob.fnCheckPrime(num);
if(bool==false)
{
System.out.println("Number is composite");
int sumdigits=ob.fnSumDigits(num);
System.out.println("Sum of digits is:"+sumdigits);
int sumfact=ob.fnSumFact(num);
System.out.println("Sum of prime facors is:"+sumfact);
if(sumdigits==sumfact)
System.out.println("Number is Smith Number");
else
System.out.println("Number is not Smith Number");
}
else
System.out.println("Number is prime so it is not a Smith Number");
}
public boolean fnCheckPrime(int num) //checks whether the number is prime or not
{
int flag=0;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
return true;
else
return false;
}
public int fnSumDigits(int num) //find out sum of digits of number
{
int n1,r1,r=0,sum=0;
do
{
n1=num/10;
r1=num%10;
r+=r1;
num=n1;
}
while(n1>=10);
sum=r+n1;
return sum;
}
public int fnSumFact(int num) //find out sum of prime factors of number
{
int num1,n,sum1=0,sum2=0,sum=0,r=0;
System.out.print("Prime factors are:");
for(int i=2;i<=num;i++)
{
if(num%i==0)
{
System.out.print(i +", ");
num=num/i;
if(i<=10)
sum1+=i;
i--;
if(i>10)
{
i++;
num1=i;
do
{
n=num1/10;
r+=num1%10;
num1=n;
if(num1<10)
{
System.out.println();
sum2=r+num1;
break;
}
}
while(num1>10);
}
}
}
sum=sum1+sum2;
return sum;
}
}
/*------------------Program developed by Nitendra Kumar-----------------*/
No comments:
Post a Comment
Ur comments r most welcome...