C++ Summing the digits in an integer
###### Posted by Samath
Last Updated: January 06, 2021

Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer in 932, the sum of all its digits is 14.

(Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932/10 = 93.)

Solution:

```#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int number;
int sep1;
int sep2;
int sep3;
int sep4;
int sum;

cout<<"Enter a number between 0 and 1000: ";
cin>>number;

if(number > 0 && number < 1000)
{
sep1 = number % 10;
sep2 = number/10;

if(number >= 100)
{
sep3 = sep2 % 10;
sep4 = sep2/10;
sum = sep1 + sep3 + sep4;
}
else
{
sum = sep1 + sep2;
}
cout<<"Total: "<<sum<<endl;
}
else
{
cout<<"Invalid Number"<<endl;
}
system("pause");

return 0;
}
```
###### Summing the Digits in an Integer using Python
Samath | Mar 02, 2015
###### Java program that reads an integer and prints how many digits the number has
Samath | Feb 11, 2021
###### Recursive function that find the sum of digits in a integer using C++
Samath | Dec 24, 2014
###### C program that prompts the user for a positive integer and then computes the sum of all the digits of the number
Samath | May 04, 2016
###### Java program that read a five-digit positive integer and breaks it into a sequence of individual digits
Samath | Feb 10, 2021
###### Summing elements in a list using Python
Samath | Mar 04, 2015
###### SQL Display two digits after decimal point
SceDev | Feb 08, 2024
###### Python program that test if integer values can form a triangle
Samath | Nov 06, 2014
###### C program that receives an integer number and determines if it is a prime number
Samath | Jun 01, 2015
###### Convert Integer to Roman Numeral using C#
Samath | Jan 20, 2024
###### C program that takes a non- negative integer number as input and prints its binary representation
Samath | Jan 01, 2017
###### C++ Convert an Integer to Roman Numeral
Samath | Jan 20, 2024
###### Java program that reads an integer and prints if it's positive or negative
Samath | Feb 11, 2021
###### C++ program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer
Samath | Apr 06, 2022