-
Notifications
You must be signed in to change notification settings - Fork 2
/
13.2 Sum of prime.java
62 lines (48 loc) · 1.38 KB
/
13.2 Sum of prime.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Santosh want to find the sum of prime number from 1 to given number, So help him to find the solution using java programing langauge.
if the number is less than 0 or greater than 1000 than the result should be display zero(0).
Input Format
Line 1: Enter the end range: 50
Constraints
1 < n >1000
Output Format
The Sum of Prime Numbers from 1 to 50 is: 328
Sample Input 0
50
Sample Output 0
328
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num>0 && num<1000)
{
int sum = 0;
for(int i=1;i<=num;i++)
{
boolean check = true;
if(i==1)
continue;
for(int j=2;j*j<=i;j++)
{
if(i%j==0)
{
check = false;
break;
}
}
if(check==true)
{
sum += i;
}
}
System.out.print(sum);
}
else
System.out.print("0");
}
}