Requirements 1) A Smith Number is a composite number the sum of whose digits is
ID: 638492 • Letter: R
Question
Requirements 1) A Smith Number is a composite number the sum of whose digits is the sum of the digits of its prime factors 2) A Smith Brother is two consecutive Smith numbers in which the difference between them is 1 3) Create a Java program that will accept as input the number of Smith numbers to be displayed and identify any Smith brothers in the process. 4) The input of quantity of Smith numbers must be verified as being an integer that is greater than or equal to 1. Display an error message to the end user that invalid input has been obtained and repeat the prompt to the end user d value of greater than or equal to 1 is obtained. 5) Examples 4 is a Smith Number since the sum of the digits are 4, the factors of 4 are 2 2 (2 2 4), and 4 is a composite number 5 is NOT a Smith number even though the sum of the digits are 5, the factor is 5, however 5 is prime (not composite) 22 is a Smith Number since the sum of the digits are 2 2 4, the factors of 22 are 2 11 (2 1 1 4), and 22 is a composite number (not prime) 27 is a Smith Number since the sum of the digits are 2 7 9, the factors are 3 3 3 (3 3 3 9), and 27 is a composite number (not prime) 6) All shop standards are to be followed as instructed in class. 7) You are not allowed to "hard code" or store the list of Smith Number in an array, collection, mapping, or array list (the program must account/calculate the Smith numbers and track display any Smith Brothers that are found) 8) Save the Java source file as "mrwJavaProgram01.java where mrw are your initials. Mock Run: Input the number of Smith numbers to be displayed sdljsajsad sdljsajsad is not valid input Input the number of Smith numbers to be displayed 1 is not valid input input the number of Smith numbers to be displayed: 0 0 is not valid input input the number of Smith numbers to be displayed: 50 Displaying the list of 50 Smith Numbers 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone{
static boolean isvalid(String s){
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) >= 48 && s.charAt(i) <= 57)
continue;
else
return false;
}
if (s.charAt(0) == '0')
return false;
return true;
}
static int sumDig(int n){
int s=0;
while(n>0){
s=s+n%10;
n=n/10;
}
return s;
}
static int sumPrimeFact(int n){
int i=2, sum=0;
while(n>1){
if(n%i==0){
sum=sum+sumDig(i);
n=n/i;
}
else
i++;
}
return sum;
}
static boolean prime(int n){
int k = 2;
while (k*k <= n){
if (n % k == 0)
return false;
k += 1;
}
return true;
}
static void print(int n){
int prev = 4;
int next = 22;
System.out.println(4);
int i = 2;
while (i <= n){
if (prime(next) == false && sumDig(next) == sumPrimeFact(next)){
System.out.print(next);
if (next-prev == 1){
System.out.println(" Smith Brother Found");
}
else{
System.out.print(" ");
}
i++;
prev = next;
}
next++;
}
}
public static void main (String[] args) throws java.lang.Exception{
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("Input the number of Smith number to be displayed: ");
String s = sc.nextLine();
if (isvalid(s) == false){
System.out.println(s+" is not a valid input ! ");
}
else{
print(Integer.parseInt(s));
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.