find sum of all prime numbers between 1 to n
Sample Output
Find sum of all prime numbers between 1 to n. Enter a number: 19 2 3 5 7 11 13 17 19 Sum of all prime numbers between 1 to 19 = 77
Java-Source Code
//package loopinjava;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int i, j, number, sum = 0, isPrime;
Scanner input = new Scanner(System.in);
System.out.println("Find sum of all prime numbers between 1 to n.\n");
System.out.print("Enter a number: ");
number = input.nextInt();
for (i = 1; i <= number; i++) {
isPrime = 1;
for (j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = 0;
}
}
if (isPrime == 1) {
if (i == 1) {
/**
* 1 is not a prime number
*/
} else {
System.out.println(i);
sum += i; // sum = sum + i;
}
}
}
System.out.println("Sum of all prime numbers between 1 to " + number + " = " + sum);
}
}
Sample Output
Find sum of all prime numbers between 1 to n. Enter a number: 7 2 3 5 7 Sum of all prime numbers between 1 to 7 = 17
No comments:
Post a Comment