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
- C Program To Find Leap Year
- C Program To Append Data Into A File
- C Program to Accept Only Integer Values
- C Program To Arrange Numbers In Descending Order
- C Program to Add Days to Date
- C Program to Add Two Fractions
- C Program To Reverse A Number
- C Program to Find Maximum and Minimum Numbers
- C Program to Read a Text File
- C Program to Convert Decimal to Hexadecimal
- C Program to Convert Decimal to Binary
- C Program to Convert Celsius to Fahrenheit
- C Program To Find Absolute Value
- Ternary Operator Program in C
- C Program For Addition Table Using For Loop
- Denomination Program in C
- C Program to Print Multiplication Table
- C Program Array with Example
- Binary Search in C Program Using Recursion
- Bubble Sort in C Using Pointers