C++ Program to Find the Sum of Digits of a Number – C++ Program.
Flowchart of Sum of Digits of a Number – C++
C++ Source Code of Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<iostream> using namespace std; int main() { int num,i,k=9; int sum=0; cout<<"Please Enter a number:"; cin>>num; i=1; do { sum=sum+k; k=k*10+9; i++; } while(i<=num); cout<<"The sum of series="<<sum; } |
Output
Please Enter a number: 5
The sum of series = 111105
Exercise
Find the possible mistakes in the following Shamilâs Flow Table of the Program to Find the Sum of Digits of a Number – C++ Program.
Loop | condition | What lines will execute | Actual work to do |
i=1 |
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 | sum=sum +k;
9=0+9 K=k*10+9; 99=9*10+9 |
|
1 time
i=2 |
True
|
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
10,11,12,13,14,15,16 |
sum=sum +k;
108=9+99 K=k*10+9; 999=99*10+9 |
2 times
i=3 |
True
|
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
10,11,12,13,14,15,16,10,11,12,13,14,15,16 |
sum=sum +k;
1107=108+999 K=k*10+9; 9999=999*10+9 |
3 time
i=4 |
False | 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
10,11,12,13,14,15,16,10,11,12,13,14,15,16,17,18 |
âsum of seriesâ |