write a function sumprime(int) to input a number and display the digit which are prime. also print the sum of the prime digits. [Java]
Answers
Solution.
The given code is written in Java.
import java.util.*;
public class Brainly{
static void sumprime(int n){
int sum=0,d;
System.out.print("The prime digits are as follows - ");
for(;n!=0;n/=10){
d=n%10;
if(d==2 || d==3 || d==5 || d==7){
System.out.print(d+" ");
sum+=d;
}
}
System.out.println("\nThe sum of the prime digits is: "+sum);
}
public static void main(String args[]){
System.out.print("Enter a number: ");
sumprime((new Scanner(System.in)).nextInt());
}
}
The function sumprime() calculates and displays the prime digits and their sum. The function is then called inside main after taking a number as input.
See attachment for verification.