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


In this program, You will learn how to find the 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 the sum of all prime numbers between 1 to n in c

#include<stdio.h>
int main() {

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

  printf("Enter a number:");
  scanf("%d", &n);

  printf("List 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 a number:10
List of prime numbers:2 3 5 7
Sum of prime numbers:17