In this post, we will learn how to find the sum of all even numbers Between 1 to N using C Programming language.

So, without further ado, let’s begin this tutorial.
C Program To Find The Sum of all Even Numbers Between 1 To N
// C Program To Print Sum of all Even Numbers From 1 To N #include <stdio.h> int main(){ int i, num, sum = 0; // Asking for Input printf("Enter the maximum value: "); scanf("%d", &num); printf("Even Numbers Between 0 and %d are: \n", num); for (i = 1; i <= num; i++){ if (i % 2 == 0){ printf("%d\n", i); sum = sum + i; } } printf("The Sum of Even Numbers from 0 to %d is %d.", num, sum); return 0; }
Output
Enter the maximum value: 15
Even Numbers Between 0 and 15 are:
2
4
6
8
10
12
14
The Sum of Even Numbers from 0 to 15 is 56.
How Does This Program Work ?
int i, num, sum = 0;
In this program, we have declared three int data type variables named as i, num and sum. The sum variable has assigned a value of 0.
// Asking for Input
printf("Enter the maximum value: ");
scanf("%d", &num);
Then, the user is asked to enter the value of the maximum number. The value of the maximum number will get stored in the num named variable.
for (i = 1; i <= num; i++){
if (i % 2 == 0){
printf("%d\n", i);
sum = sum + i;
}
}
Then, we have used for loop to make sure that the number lies between 1 and maximum value.
We check whether the value of i is divisible by 2 or not, If yes then that iteration of i is an even number. We will add print and add the value of i to sum.
printf("The Sum of Even Numbers from 0 to %d is %d.", num, sum);
Finally, the sum is printed to the screen using the printf() function.
C Program To Find The Sum of all Even Numbers Without Using If Statement
// C Program To Find The Sum of all Even Numbers From 1 To N #include <stdio.h> int main(){ int i, num, sum = 0; // Asking for Input printf("Enter the maximum value: "); scanf("%d", &num); printf("Even Numbers From 0 to %d are: \n", num); for (i = 2; i <= num; i = i + 2){ printf("%d\n", i); sum = sum + i; } printf("The Sum of all Even Numbers Between 0 to %d is %d.", num, sum); return 0; }
Output
Enter the maximum value: 19
Even Numbers From 0 to 19 are:
2
4
6
8
10
12
14
16
18
The Sum of all Even Numbers Between 0 to 19 is 90.
C Program To Find The 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, min, max, sum = 0; // Asking for Input printf("Enter the minimum value: "); scanf("%d", &min); printf("Enter the maximum value: "); scanf("%d", &max); printf("Even Numbers Between %d and %d are: \n", min, max); for (i = min; i <= max; i++){ if (i % 2 == 0){ printf("%d\n", i); sum = sum + i; } } printf("The Sum of Even Numbers from %d to %d is %d.",min, max, sum); return 0; }
Output
Enter the minimum value: 11
Enter the maximum value: 23
Even Numbers Between 11 and 23 are:
12
14
16
18
20
22
The Sum of Even Numbers from 11 to 23 is 102.
Conclusion
I hope after going through this post, you understand how to print the sum of all even numbers using the C programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: