Overview:
First of all in this article, I want to introduce you to numbers and its digits. Then I will demonstrate how to find the sum of its digits with the Logic to find the sum of digits. After all, I will write c program to print the sum of the digits of a number. After all, I will show the 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 then its sum of digits will be 1+2+4+3 = 10 Another example, Consider integer 9876 then the sum of digits = 9+8+7+6 = 30
Logic to find the sum of digits of a number
To get some of each digit by c program, use the following logic:
- Step 1: Get number by the user, store it into variable number
- Step 2: Get the remainder of the number by using the modulus operator
- Step 3: sum up the remainder of the number of each step
- Step 4: Divide the number by 10
- Step 5: Repeat step 2 while the 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.