sum of prime numbers in c code example

Example 1: prime numbers in c

#include<stdio.h>
main() {
    int n, i, count=0;
    printf("Enter Number");
    scanf("%d",&n);
   
    for ( i = 1; i<= n; i++) {
        if (n % i == 1) {
            count++;
        }
    }
    if( count == 2) {
        printf("Prime Number");
    }
    else {
        printf("Not A  Prime Number");
    }
}

Example 2: program to find sum of first n prime numbers in c

#include <stdio.h>
int isprime(int j) {
   int count=0;
   for(int i = 2 ; i <= j/2; i++) {
      if(j%i == 0) {
         count = 1;
      }
   }
   if(count == 0) {
      return 1;
   }
   else
      return 0;
}
int main(void) {
   int n = 5;
   int i=0, j= 1;
   int sum = 0;
   while(1) {
      j++;
      if(isprime(j)) {
         sum += j;
         i++;
      }
      if(i == n) {
         break;
      }
   }
   printf("The sum of first %d prime numbers is %d", n, sum);
   return 0;
}

Example 3: Q5.WAP tofind out the sum of all prime numbers between 1 and n by using a user defined function (say isPRIME) to be used for prime number testing, where n is a value supplied by the user.

Q5.WAP tofind out the sum of all prime numbers between 1 and n by using a user defined function (say isPRIME) to be used for prime number testing, where n is a value supplied by the user.

Tags:

C Example