+ 2
Write a program to input a number & find out whether the sum of the digits in the number is a prime number or not.
this has to be done using java in while loop. i tried some methods using / and % but I ain't able to do it right. any one willing to send me a detailed solution
4 Answers
+ 15
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
boolean f = true;
int sum = 0, i = 2;
System.out.println("Enter the number:");
int n = new Scanner(System.in).nextInt();
while(n != 0)
{
sum += n % 10;
n /= 10;
}
while(i <= sum / 2)
{
if(sum % i == 0)
{
f = false;
}
++i;
}
if(f)
System.out.println("The sum of the digits in the entered number is a prime number.");
else
System.out.println("The sum of the digits in the entered number is not prime number.");
}
}
+ 2
thnks to everyone who helped me
+ 1
https://code.sololearn.com/cF4MVIYnD40U/?ref=app