Home » C Programs » C Program to Find Sum of All Prime Numbers From 1 to n

# C Program to Find Sum of All Prime Numbers From 1 to n

Basic c program to find sum of all prime numbers from 1 to n. In this program to check sum of all prime numbers in c programming language.

## Prime Number Definition

A prime number is a natural number larger than one that cannot be multiplied by two lesser natural numbers. A composite number is a natural number greater than one that is not prime.

A prime number is a whole number bigger than one with just the number one and itself as factors. A factor is a whole number that can be divided into another number in an even number.

## For Loop Definition in C

In C, the for loop is used to iterate over statements or parts of programs numerous times. It is commonly used to traverse data structures such as arrays and linked lists.

A “For” Loop is used to iterate over a specified block of code a predetermined number of times. The for statement allows you to specify the number of times a statement or compound statement should be repeated. A for statement’s body is run zero or more times until an optional condition is met. Here we have executed the c program to find sum of all prime numbers from 1 to n using for loop

## C Program to Find Sum of All Prime Numbers From 1 to n

``````#include <stdio.h>
int main()
{
int i, j, end, isPrime, sum=0;
printf("Sum of Prime Number");
scanf("%d", &end);
for(i=2; i<=end; i++)
{
isPrime = 1;
for(j=2; j<=i/2 ;j++)
{
if(i%j==0)
{
isPrime = 0;
break;
}
}
if(isPrime==1)
{
sum += i;
}
}
printf("Sum of All Prime Number 1 to N %d = %d", end, sum);
return 0;
}``````

Output:

Sum of Prime Number45
Sum of All Prime Number 1 to N 45 = 281