In this tutorial, you will learn how to write a program to print sum of all odd numbers between 1 to n using a while loop in C. Here, we will see examples, algorithms, logics and program explanation for your better understanding in C programming language.
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 should take a positive integer ‘n’ as input. After taking the input it should print the sum of all odd numbers between 1 and n using a while loop. The program provides a solution to find the sum of odd numbers using a while loop in the C programming language. You can refer to the below example to understand better what we have to achieve. The first value and n will be not included in the operation.
Example
Input:
Enter the value of n: 7
Output:
The sum of odd numbers between 1 and 7 is: 8
In the above example, user given input is 7. So the odd number between 1 to 7 is 1, 3, 5 and 7. So the output is 16.
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 odd 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 odd using the modulo operator (num % 2 != 0
). - If it is odd, add
num
tosum
. - Increment
num
by 1.
- Check if the current number
- Exit the while loop.
- Print the sum of odd numbers as: “The sum of odd 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 odd 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 or equal to the given number (n).
- Inside the loop, check if the num is an odd number using the modulo (%) operator.
- If the num is odd, add it to the sum variable.
- Increment the num by 1 in each iteration.
- Continue the loop until the counter reaches the given number (n).
- Print the sum of odd numbers.
- Finish the program.
Print the sum of all odd 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 odd numbers between 1 and %d is: %d\n", n, sum);
return 0;
}
Output
Enter the value of n: 12
The sum of odd numbers between 1 and 12 is: 36
Program Explanation
- The program takes input for the value of n from the user.
- It initializes a variable
num
to 1, which represents the first odd number to be considered for summation. - It also initializes a variable
sum
to 0, which will hold the running sum of odd numbers. - The program enters a while loop with the condition
num <= n
to iterate through numbers from 1 to n. - Inside the while loop, it checks if
num
is an odd number. - If
num
is odd, it adds the value ofnum
tosum
. - It then increments the value of
num
by 2 since the next odd 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
sum
, which represents the sum of all odd numbers between 1 and n. - The program ends.
Conclusion
The C program successfully calculates and prints the sum of all odd 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 odd numbers, calculates their sum, and displays the result on the screen.
I hope this article will be helpful to understand this program. Please do share.