-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfPrimeNumbersm_n.java
More file actions
43 lines (42 loc) · 966 Bytes
/
SumOfPrimeNumbersm_n.java
File metadata and controls
43 lines (42 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package number_Programming.medium.day_02;
import java.util.*;
//wap to print the sum of prime numbers form range m to n
public class SumOfPrimeNumbersm_n
{
public static boolean checkPrime(int num)
{
if(num<2)
return false;
for(int i = 2;i<=num/2;i++)
{
if(num % 2 == 0)
return false;
}
return true;
}
public static int sumOfPrime(int m ,int n)
{
int sum = 0;
System.out.println("The prime numbers are :");
for(int i = m;i<=n;i++)
{
if(checkPrime(i))
{
System.out.println(i + " ");
sum = sum + i;
}
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the upper bound : ");
int m = sc.nextInt();
System.out.println("Enter the lower bound : ");
int n = sc.nextInt();
int sum = sumOfPrime(m, n);
System.out.println("Sum of the prime numbers present in the range of " +
m + " and " + n + " is " + sum);
sc.close();
}
}