WAP to find sum of all prime number between 1 to n
Write a C
program to find sum of all prime numbers between 1 to n using for loop. C
program to generate sum of all primes between a given range. Logic to find sum
of prime numbers in a given range.
Example
Input
Input upper limit: 10
Output
Sum of prime numbers between 1-10: 17
Prime
numbers are the positive integers greater than 1 that has only two divisors 1
and the number itself. For example: 2, 3, 5, 7, 11 are the first 5 prime
numbers.
Logic
to find sum of prime numbers between 1 to n
Finding sum of prime
number is just a 7 std kid task, if you know checking prime numbers. Before I
get to the formal logic of the program. I recommend you to learn the below two
concepts before moving on.
·
Program to print prime numbers between
1 to n.
Below is the step by
step descriptive logic to find sum of prime numbers between 1 to n.
1. Read
upper limit to find sum of prime from user. Store it in some variable say n.
2. Initialize
another variable sum = 0 to
store sum of prime numbers.
3. Run
a loop from 2 to n, incrementing 1 in
each loop counter. The loop structure should look like for(i=2; i<=n; i++).
4. Inside
this loop check if i is prime.
Then increment the sum variable by
1 i.e. sum = sum + 1.
5. Finally
after loop print the resultant value of sum.
Program
to find sum of prime numbers between 1 to n
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
|
/**
*
C program to find sum of prime numbers between 1 to n
*/
#include
<stdio.h>
int
main()
{
int
i, j, n, isPrime, sum=0;
/*
*
Read upper limit from user
*/
printf("Find
sum of all prime between 1 to : ");
scanf("%d",
&n);
/*
*
Find all prime numbers between 1 to n
*/
for(i=2;
i<=n; i++)
{
/*
*
Check if the current number i is Prime or not
*/
isPrime
= 1;
for(j=2;
j<=i/2 ;j++)
{
if(i%j==0)
{
isPrime
= 0;
break;
}
}
/*
*
If i is Prime then add to sum
*/
if(isPrime==1)
{
sum
+= i;
}
}
printf("Sum
of all prime numbers between 1 to %d = %d", n, sum);
return
0;
}
|
Program
to find sum of prime numbers in given range
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
|
/**
* C program to find sum of prime
numbers in given range
*/
#include <stdio.h>
int main()
{
int i, j, start,
end;
int isPrime, sum=0;
/*
* Read lower
and upper limit from user
*/
printf("Enter
lower limit: ");
scanf("%d",
&start);
printf("Enter
upper limit: ");
scanf("%d",
&end);
/*
* Find all
prime numbers in given range
*/
for(i=start;
i<=end; i++)
{
/*
*
Check if the current number i is Prime or not
*/
isPrime
= 1;
for(j=2;
j<=i/2 ;j++)
{
if(i%j==0)
{
isPrime
= 0;
break;
}
}
/*
*
If i is Prime then add to sum
*/
if(isPrime==1)
{
sum
+= i;
}
}
printf("Sum of
all prime numbers between %d to %d = %d", start, end, sum);
return 0;
}
|
Output
Enter lower limit: 10
Enter upper limit: 20
Sum of all prime numbers between 10 to
20=60
|
Comments
Post a Comment