Write a program to input a number & find out whether the sum of the digits in the number is a prime number or not. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

1st Oct 2017, 9:51 AM
Aaron Stone
Aaron Stone - avatar
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."); } }
1st Oct 2017, 10:34 AM
Vukan
Vukan - avatar
+ 2
thnks to everyone who helped me
1st Oct 2017, 6:10 PM
Aaron Stone
Aaron Stone - avatar
1st Oct 2017, 11:14 AM
Jonas Schröter
Jonas Schröter - avatar
+ 1
https://code.sololearn.com/cF4MVIYnD40U/?ref=app
1st Oct 2017, 11:15 AM
Rashid