C program that prompts the user for a positive integer and then computes the sum of all the digits of the number
Posted by Samath
Last Updated: May 04, 2016

Write a program (addDigits.c) that prompts the user for a positive integer and then computes the sum of all the digits of the number. For example, if the user enters 2784, then the program reports 21. If the user enters 59, then the program reports 14. The program should work for any number having one to ten digits.

Solution:

```#include <stdio.h>
int main(int argc, char *argv[])
{
int num=0;
int sum=0;
int sep=0;
printf("Please enter a positive integer: ");
scanf("%d",&num);

while(num > 0)
{
sep = num%10;
num = num/10;
sum = sum + sep;
}

printf("Result: %d\n",sum);
return 0;
}```

Recursive function that find the sum of digits in a integer using C++
Samath | Dec 24, 2014
Java program that read a five-digit positive integer and breaks it into a sequence of individual digits
Samath | Feb 10, 2021
Java program that reads an integer and prints how many digits the number has
Samath | Feb 11, 2021
C++ Summing the digits in an integer
Samath | Jan 06, 2021
Summing the Digits in an Integer using Python
Samath | Mar 02, 2015
Java program that reads an integer and prints if it's positive or negative
Samath | Feb 11, 2021
Java program that prints the sum of the first ten positive integers
Samath | Jan 08, 2021
Java program that uses a loop to compute the sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)
Samath | Apr 03, 2022
Java program that computes the sum, difference, product, average, distance, maximum and minimum of two numbers
Samath | Feb 08, 2021
C++ program that prompts the user to input five decimal numbers, prints the five decimal numbers, converts each decimal number to the nearest integer, adds the five integers and prints the sum and average of the five integers.
Samath | Apr 06, 2022
Sum of two values in C
Samath | Nov 09, 2014
C Program to find sum of two numbers
Samath | Jan 02, 2017
C++ Calculate the sum from 1 to 100
Samath | Jan 05, 2017
Java program that asks the user for a year and computes whether that year is a leap year
Samath | May 08, 2021