C Program To Add Digits Of A Number

C Program To Add Digits Of A Number.  If you are looking find sum of the digits of a number program in C, here in this tutorial we will help you to learn how to write a C program to compute sum of digits in a given number.

C Program to Add Digits of a Number

Learn how to write a C program to find sum of digits of a number. Writing C Program to add digits of a given number can be done using various techniques but here in this program, we show how to write a C program to find sum of digits of a number using while loop in a proper way.

How do you find the sum of digits in C?

If a user enters a number 1234, then we have to write a logic to calculate sum of the digits of 1234. That is 1 + 2 + 3 + 4 = 10. The program should read the individual digits and then it should add digits and print sum of the digits in a given number.

C Program to Compute Digits of a Number Source Code

Just copy paste the below source code to find sum of the digits of a number in C compiler to test, how the source code works. Happy Learning.

/* C program to add digits of a number - AddDigits.C */

#include <stdio.h>
 
void main()
{
 long num, temp, digit, sum = 0;
 
 printf("Enter the number to find sum of the digits: \n");
 scanf("%ld", &num);
 temp = num;
 while (num > 0)
 {
  digit = num % 10;
  sum = sum + digit;
  num /= 10;
 }
 printf("Given number is = %ld\n", temp);
 printf("Sum of the digits of %ld = %ld\n", temp, sum);
}

C Program to Find Sum of Digits of a Number Output

Run the above program to add digits of a given number to print sum of digits. For this enter a number to check, the c compiler will execute the logic and display the sum of the digits of a number.

Output:

Enter the number to find sum of the digits: 1234

Given number is = 1234

Sum of the digits of 1234 = 10

C PROGRAMMING EXAMPLES

  1. C Program To Find Leap Year
  2. C Program To Append Data Into A File
  3. C Program to Accept Only Integer Values
  4. C Program To Arrange Numbers In Descending Order
  5. C Program to Add Days to Date
  6. C Program to Add Two Fractions
  7. C Program To Reverse A Number
  8. C Program to Find Maximum and Minimum Numbers
  9. C Program to Read a Text File
  10. C Program to Convert Decimal to Hexadecimal
  11. C Program to Convert Decimal to Binary
  12. C Program to Convert Celsius to Fahrenheit
  13. C Program To Find Absolute Value
  14. Ternary Operator Program in C
  15. C Program For Addition Table Using For Loop
  16. Denomination Program in C
  17. C Program to Print Multiplication Table 
  18. C Program Array with Example
  19. Binary Search in C Program Using Recursion
  20. Bubble Sort in C Using Pointers

 

Leave a Reply

Your email address will not be published. Required fields are marked *