A friend of mine who is learning to program in C, was having diffculty in writing a program to find the sum of all the prime numbers between a specified limit (in her case 3-60). I helped her out with the program, and I thought I'll post it here which might help others.
Code: C
/*
** Program to find the sum of all numbers between 3 and 60
** @author : Pradeep
** @date : 11/29/2006
*/
#include <stdio.h>
void main(void)
{
unsigned int i,j,s=0,is_prime;
for(i=3;i<=60;i++)
{
is_prime = 1; // Assuming that current value of i is prime
// Checking for prime
for(j=2;j<=(i/2);j++)
{
if(i%j==0) // Not prime, set is_prime to 0 and break
{
is_prime = 0;
break;
}
}
if(is_prime) // If the number is prime, sum it up
{
s += i;
// optionally you can print the prime numbers too
printf("%d ",i);
}
}
printf("\n\nThe sum of the prime numbers = %d",s);
}


