Computer Science, asked by jaina4281, 5 hours ago

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

Answered by anindyaadhikari13
2

Solution.

The given co‎‎de 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 fu‎‎nction sumprime() calculates and displays the prime digits and their sum. The fu‎‎nction is then called inside main after taking a number as input.

See attachment for verification.

Attachments:
Similar questions