-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.cpp
More file actions
26 lines (21 loc) · 746 Bytes
/
12.cpp
File metadata and controls
26 lines (21 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// C++ program to find the sum of all the digits of a number using while statement.
#include <iostream>
using std::cin, std::cout;
int main()
{
// declaring the required variables.
int number, tempVariable, remainder, sum = 0;
// entering the values for the variables from the console.
cout << "Enter a number: ";
cin >> number;
// calculating the sum of all the digits of the given number.
tempVariable = number;
while(number != 0)
{
remainder = number % 10;
sum += remainder;
number /= 10;
}
// printing the sum of all the digits of the given number to the console.
cout << "Sum of all digits present in " << tempVariable << " is: " << sum;
}