How to write a C Program to Print Sum of all Even Numbers using If Statement with an example.
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 finds the Sum of all Even Numbers in a given range.
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 */ #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; }
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 checks 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 C Programming 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 the 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("\nPlease Enter the Maximum Limit Value :"); scanf("%d", &number); printf("\nEven Numbers between 0 and %d are : ", number); for(i = 2; i <= number; i=i+2) { Sum = Sum + i; printf("%d ", i); } printf("\nThe Sum of All Even Numbers upto %d = %d", number, Sum); return 0; }
C sum of even numbers from 1 to n using for loop output
Please Enter the Maximum Limit Value :20
Even Numbers between 0 and 20 are : 2 4 6 8 10 12 14 16 18 20
The Sum of All Even Numbers upto 20 = 110
Program to Print Sum of all Even Numbers in a Given Range
This C 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("\nPlease Enter the Minimum, and Maximum Limit Values :\n"); scanf("%d %d", &Minimum, &Maximum); printf("\nEven Numbers between %d and %d are : ", Minimum, Maximum); for(i = Minimum; i <= Maximum; i+=2) { printf("%d ", i); Sum = Sum + i; } printf("\n\nThe Sum of All Even Numbers betwen %d and %d = %d", Minimum, Maximum, Sum); return 0; }
Please Enter the Minimum, and Maximum Limit Values :
10
100
Even Numbers between 10 and 100 are : 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
The Sum of All Even Numbers betwen 10 and 100 = 2530