Answer to Question #249156 in Java | JSP | JSF for ranju

Question #249156

.Write a program to find the sum of all the prime numbers in the range n to m. Display each prime number and the final sum. 


1
Expert's answer
2021-10-09T15:50:42-0400
import java.util.Scanner;


public class Main {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		int n=0;
		while(n<=1) {
			System.out.print("Enter n>1: ");
			n = keyBoard.nextInt();
		}
		int m=0;
		while(m<=n) {
		System.out.print("Enter m>n: ");
			m = keyBoard.nextInt();
		}
		int sum = 0;
		for (int number = n; number <= m; number++) {
			boolean is_Prime = true;
			for (int i = 2; i <= number / 2; i++) {
				if (number % i == 0) {
					is_Prime = false;
					break;
				}
			}
			if (is_Prime) {
				System.out.print(number + " ");
				sum += number;
			}
		}
		System.out.println("\nThe sum of all the prime numbers: " + sum);
		keyBoard.close();


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS