Overview:
First of all in this article, I want to introduce you with number and its digits. Then I will demonstrate how to find the sum of its digits with the Logic to find sum of digits. After all, I will write c program to print the sum of digits of a number. After all, I will show Program output.
Table of contents:
- What is Number?
- Demonstration How to find the sum of its digits?
- Logic to find the sum of digits of a number
- C program to print the sum of digits of a number
- Conclusion
What is Number?
Number is an arithmetical value, expressed by a word, symbol, or figure, representing a particular quantity and used in counting and making calculations. For example, 1243 has 4 symbols so we can say that 1243 is 4 digits number.
Demonstration How to find the sum of its digits?
Suppose, We can consider 1243 as a number than its sum of digits will be 1+2+4+3 = 10 Other example, Consider integer 9876 then sum of digits = 9+8+7+6 = 30
Logic to find the sum of digits of a number
To get sum of each digits by c program, use the following logic:
- Step 1: Get number by user, store it into variable number
- Step 2: Get the remainder of the number by using modulus operator
- Step 3: sum up the remainder of the number of each step
- Step 4: Divide the number by 10
- Step 5: Repeat the step 2 while number is greater than 0.
C program to print the sum of digits of a number
#include <stdio.h> int main() { int number,sum=0,m; printf("Enter a number of any digit:"); scanf("%d",&number); while(number>0) { m=number%10; sum=sum+m; number=number/10; } printf("Sum of digits is = %d" , sum); return 0; }
Conclusion:
Number is an arithmetical value, expressed by a word, symbol, or figure, representing a particular quantity and used in counting and making calculations. In this Program first of all User Enter the an integer number then Program display the sum of digits of inputed number as an output.