C Programming Code Examples C > For Loops and While Loops Code Examples find sum of prime numbers in given range find sum of prime numbers in given range 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. What is Prime number? Prime numbers are 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. #include <stdio.h> int main() { int x, j, start, end; int isPrime, sum=0; /* Input 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(x=start; x<=end; x++) { /* Check if the current number x is Prime or not */ isPrime = 1; for(j=2; j<=x/2 ;j++) { if(x%j==0) { /* 'x' is not prime */ isPrime = 0; break; } } /* * If x is Prime then add to sum */ if(isPrime==1) { sum += x; } } printf("Sum of all prime numbers between %d to %d = %d", start, end, sum); return 0; }