In this tutorial, you will learn how to write a C program to print the sum of digits of a given number using a while loop. Here, we will explore examples, logics, algorithms and program explanations for a better understanding of your 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 an integer number as input. After taking the input it should calculate the sum of its digits using a while loop. The program should provide a solution that will print the sum of the digits of a number using a while loop in the C programming language.
For a better understanding of what we have to achieve, below you have an example.
Example
Input:
Enter a number: 12345
Output:
The sum of the digits in 12345 is: 15
In the above example, you can see that the input number is 12345. And if we perform the sum then the output will be 15.
Algorithm
- Start the program.
- Declare the variables:
num
to store the input number,sum
to store the sum of digits, anddigit
as a temporary variable to hold each digit. - Read the value of
num
from the user. - Initialize
sum
to 0. - Enter the while loop with the condition
num != 0
.- Extract the last digit of
num
using the modulo operator%
and assign it todigit
. - Add
digit
tosum
. - Divide
num
by 10 to remove the last digit.
- Extract the last digit of
- Exit the while loop.
- Print the sum of digits as: “The sum of the digits in num is: sum”.
- End the program.
Logic used to solve this problem
- Declare a variable to hold the input number and another variable to store the sum of digits.
- Prompt the user to enter a number.
- Read and store the entered number in the variable.
- Initialize the sum variable to 0.
- Use a while loop that runs as long as the number is greater than 0.
- Inside the loop, extract the last digit of the number using the modulo (%) operator.
- Add the extracted digit to the sum variable.
- Reduce the number by removing the last digit using the integer division (/) operator.
- Continue the loop until the number becomes 0.
- Print the sum of the digits.
- Finish the program.
Program to print the sum of digits of a given number
#include <stdio.h>
int main() {
int num, digit, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("The sum of the digits is: %d\n", sum);
return 0;
}
Output
Enter a number: 23
The sum of the digits is: 5
Program Explanation
- The program takes input for the number whose digits’ sum is to be calculated.
- It initializes a variable
sum
to 0, which represents the running sum of digits. - The program enters a while loop with the condition
number > 0
to extract the digits of the given number. - Inside the while loop, it calculates the remainder of the number divided by 10.
- It adds the value of the remainder to
sum
. - It updates the value of the number by dividing it by 10, effectively removing the rightmost digit.
- The loop continues until the number becomes 0, indicating that all digits have been processed.
- Once the while loop is complete, the program prints the value of
sum
, which represents the sum of the digits of the given number. - The program ends.
This program effectively calculates and prints the sum of the digits of a given number using a while loop.
Conclusion
The C program successfully calculates and prints the sum of the digits of a given number using a while loop. By extracting each digit and adding it to a sum variable, it iterates through the digits of the number. The program then displays the sum of digits on the screen.