C Program to find sum of all prime numbers between 1 to n


In this program, You will learn how to find sum of all prime numbers between 1 to n in c.


Sum of prime numbers upto 10th : 2 + 3 + 5 + 7 = 17


Example: How to find sum of all prime numbers between 1 to n in c

Run it Now
#include<stdio.h>

int main() {

   int n, i, j, p, s = 0;

   printf("Enter upto Prime Numbers :");
   scanf("%d", &n);

   printf("\nList of Prime Numbers :");

   for (i = 2; i < n; i++) {
       p = 1;
       for (j = 2; j < i; j++) {
           if (i % j == 0) {
               p = 0;
               break;
           }
       }
       if (p == 1) {
           printf("%d ", i);
           s = s + i;
       }
   }
   printf("\nSum of Prime Numbers :%d", s);

   return 0;
}

Output:

Enter upto Prime Numbers :10
List of Prime Numbers :2 3 5 7
Sum of Prime Numbers :17