WAP to Input a number and display all the prime digits and their sum. E.g. 347, output 3, 7 and sum = 10
Answers
Answered by
10
WAP to input a number and display all the prime digits and print their sum.
Example: 347
Output:
3, 7 and sum = 10
Required Answer:-
//This is done in Java
import java.util.*;
class sumdigits{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int s=0;
while(n!=0){
int d=n%10;
int c=0;
for(int I=1;I<=d;I++){
if (d%I==0)
c++;
}
if (c==2){
System.out.print(d+" ");
s+=d;
}
n/=10;
}
System.out.println("Sum:"+s);
}
}
•Output Attached
Attachments:
Similar questions