Tuesday, July 16, 2019

Smith Number | Programming | Eclipse JAVA | Follow on Youtube for more



import java.util.Scanner;
public class smith_number
{
public static int sum(int number)
{
int sum = 0;
while(number!=0)
{
int mod = number%10;
sum = sum+mod;
number = number/10;
}
return sum;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int n1 = n;
int check = 2;
int result1 = sum(n);
int result2 = 0;
while(n!=0)
{
if(n%check==0)
{
n = n/check;
result2  = result2+sum(check);
}
else if(check>n1)
break;
else
check++;
}
if(result1==result2)
System.out.println(n1+" is a Smith Number");
else
System.out.println(n1+" is not a Smith Number");
sc.close();
}
}
/*
Input  : n = 4
Output : Yes
Prime factorization = 2, 2  and 2 + 2 = 4
Therefore, 4 is a smith number

Input  : n = 6
Output : No
Prime factorization = 2, 3  and 2 + 3 is
not 6. Therefore, 6 is not a smith number

Input   : n = 666
Output  : Yes
Prime factorization = 2, 3, 3, 37 and
2 + 3 + 3 + (3 + 7) = 6 + 6 + 6 = 18
Therefore, 666 is a smith number

Input   : n = 13
Output  : No
Prime factorization = 13 and 13 = 13,
But 13 is not a smith number as it is not
a composite number
*/

No comments:

Post a Comment

ICSE COMPUTER SCIENCE SPECIMEN PAPER | QUESTION 7 | PLEASE DO WATCH THE VIDEO

  import  java.util.*; class icse_specimen_1 {     public static void main(String[] args)     {         Scanner sc1 = new Scanner(System.in)...