Coding Practice

Write a C program to find sum of all prime numbers between 1 to n

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: 7

2

3

5

7

Sum of all prime numbers between 1 to 7 = 17
Source Code
#include<stdio.h>

int main()
{
    int i, j, num, sum = 0, isPrime;

    printf("Find sum of all prime numbers between 1 to n.\n\n");
    printf("Enter a number: ");
    scanf("%d", &num);

    for(i = 1; i <= num; 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
            {
                printf("\n%d\n", i);
                sum += i; // sum = sum + i;
            }
        }
    }
    printf("\nSum of all prime numbers between 1 to %d = %d\n", num, sum);
}
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

No comments:

Post a Comment

Change Theme
X