-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp-6-28.c
57 lines (50 loc) · 1.06 KB
/
exp-6-28.c
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
/*************************************************************************
> File Name: exp-6-28.c
> Author: xiaoxiaoh
> Mail: [email protected]
> Created Time: Tue Jul 11 11:31:35 2017
************************************************************************/
/*
* Write a C program to find sum of all prime numbers in given range.
*
* Example
*
* Input
* Input lower limit: 1
* Input upper limit: 10
*
* Output
* Sum of prime numbers between 1-10: 17
*
*/
#include <stdio.h>
int main()
{
int i, j, start, end, isPrime, sum=0;
//Read a lower limit from user
printf("Enter a lower limit: ");
scanf("%d", &start);
//Read a upper limit from user
printf("Enter a upper limit: ");
scanf("%d", &end);
//Find all prime number between 1 and n
for(i=start; i<=end; i++)
{
//Asumme current number is Prime
isPrime = 1;
for(j=2; j<=i/2; j++)
{
if(i%j == 0)
{
isPrime = 0;
break;
}
}
if(isPrime == 1)
{
sum += i;
}
}
printf("The sum of all prime numbers between %d and %d is %d\n", start, end, sum);
return 0;
}