c program sum of digits

devquora
devquora

Posted On: Jan 27, 2023

c program sum of digits

 

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:

  1. What is Number?
  2. Demonstration How to find the sum of its digits?
  3. Logic to find the sum of digits of a number
  4. C program to print the sum of digits of a number
  5. 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

Demo for Sum of digits of an integer

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;  
}   

Output of the Sum of digits of a number

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.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..