In this tutorial, you will learn how to write a program in C to print sum of all even numbers between 1 to n using while loop. Here, we will go through examples, algorithms, logic, and program explanation for your better understanding of this problem.
Required Knowledge
- C Programming Basics
- C Programming Operators
- C printf and Scanf
- While loop in C
Problem Statement
You have to Write a C program that takes a positive integer ‘n’ as input from the users. After taking the input, the program should print the sum of all even numbers between 1 and n using a while loop. The program provides a solution to find the sum of even numbers using a while loop in the C programming language. You can refer to the below example to understand what we have to achieve.
Example
Input:
Enter the value of n: 10
Output:
The sum of even numbers between 1 and 10 is: 20
In the above example, you can see that the user has given input Integer 10. From 1 to 10, there are four variables that are even numbers 2, 4, 6, and 8. The sum of these even numbers is 20, so the output will be 20.
Algorithm
- Start the program.
- Declare the variables:
n
to store the input value,num
to represent the current number, andsum
to store the sum of even numbers. - Read the value of
n
from the user. - Initialize
num
to 1 andsum
to 0. - Enter the while loop with the condition
num < n
.- Check if the current number
num
is even using the modulo operator (num % 2 == 0
). - If it is even, add
num
tosum
. - Increment
num
by 1.
- Check if the current number
- Exit the while loop.
- Print the sum of even numbers as: “The sum of even numbers between 1 and n is: sum”.
- End the program.
Logic used to solve this problem
- Declare a variable to hold the input number.
- Prompt the user to enter a number.
- Read and store the entered number in the variable.
- Initialize a variable to hold the sum of even numbers and set it to 0.
- Initialize a num variable to 1.
- Use a while loop that runs as long as the num is less than to the given number (n).
- Inside the loop, check if the num is an even number using the modulo (%) operator.
- If the num is even, add it to the sum variable.
- Increment the num by 1 in each iteration.
- Continue the loop until the num reaches the given number (n).
- Print the sum of even numbers.
- Finish the program.
Program to print the sum of all even numbers between 1 to n
#include <stdio.h>
int main() {
int n, num = 1, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
while (num < n) {
if (num % 2 == 0) {
sum += num;
}
num++;
}
printf("The sum of even numbers between 1 and %d is: %d\n", n, sum);
return 0;
}
Output
Enter the value of n: 12
The sum of even numbers between 1 and 12 is: 30
Program Explanation
- The program takes input for the value of n from the user.
- It initializes a variable
num
to 2, which represents the first even number to be considered for summation. - It also initializes a variable
sum
to 0, which will hold the running sum of even numbers. - The program enters a while loop with the condition
num < n
to iterate through numbers from 2 to n. - Inside the while loop, it checks if
num
is an even number. - If
num
is even, it adds the value ofnum
tosum
. - It then increments the value
num
by 2 since the next even number will be 2 greater than the current one. - The loop continues until
num
exceeds the value of n. - Once the while loop is complete, the program prints the value of, which represents the sum of all even numbers between 1 and n.
- Here n is not inclusive in the addition.
- The program ends.
Conclusion
The C program successfully calculates and prints the sum of all even numbers between 1 and the given input n
using a while loop. By iterating through the numbers and checking their divisibility by 2, it identifies the even numbers, calculates their sum, and displays the result on the screen. I hope you liked our tutorial. Please do share.