How to write a C Program to Print Sum of all Even Numbers using If Statement with an example.

**TIP:** We already explained the logic to check whether the given is Even or Not in C Program to Check Odd or Even article.

## C Program to Print Sum of all Even Numbers from 1 to n

This program allows the user to enter the maximum limit value. Next, this C Program find Sum of all Even Numbers in a given range.

/* C Program to Print Sum of all Even Numbers from 1 to N */ #include<stdio.h> int main() { int i, number, Sum = 0; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Even Numbers between 0 and %d are : ", number); for(i = 1; i <= number; i++) { if ( i%2 == 0 ) //Check whether remainder is 0 or not { printf("%d ", i); Sum = Sum + i; } } printf("\n The Sum of All Even Numbers upto %d = %d", number, Sum); return 0; }

**OUTPUT**

**ANALYSIS**

Within this C Program to find Sum of all Even Numbers from 1 to N , For Loop will make sure that the number is between 1 and maximum limit value.

for(i = 1; i <= number; i++)

In the Next line, We declared the If statement

if ( number % 2 == 0 )

Any number that is divisible by 2 is even number. If condition will check whether the remainder of the number divided by 2 is exactly equal to 0 or not. If the condition is True then it is Even number, and the compiler will add i value to sum.

## C Program to Print Sum of all Even Numbers from 1 to n

This program to find Sum of all Even Numbers is same as above but we altered for loop to eliminate the If statement.

/* C Program to Print Sum of all Even Numbers from 1 to N */ #include<stdio.h> int main() { int i, number, Sum = 0; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Even Numbers between 0 and %d are : ", number); for(i = 2; i <= number; i=i+2) { Sum = Sum + i; printf("%d ", i); } printf("\n The Sum of All Even Numbers upto %d = %d", number, Sum); return 0; }

**OUTPUT**

## Program to Print Sum of all Even Numbers in a Given Range

This program allows the user to enter Minimum and maximum value. Next, the C program will calculate the sum of even numbers between Minimum value and maximum value.

/* C Program to Print Sum of all Even Numbers in a Given Range */ #include<stdio.h> int main() { int i, Minimum, Maximum, Sum = 0; printf("\n Please Enter the Minimum, and Maximum Limit Values : \n"); scanf("%d %d", &Minimum, &Maximum); printf("\n Even Numbers between %d and %d are : ", Minimum, Maximum); for(i = Minimum; i <= Maximum; i+=2) { printf("%d ", i); Sum = Sum + i; } printf("\n\n The Sum of All Even Numbers betwen %d and %d = %d", Minimum, Maximum, Sum); return 0; }

**OUTPUT**