0

Write and run a program that reads a six digit integer and prints the sum of its six digits?

3rd Dec 2017, 9:11 AM
alikhan124
alikhan124 - avatar
3 Answers
+ 9
@AliKhan124, the example from @Uvaish Zafri is correct, it is how you would calculate number of digits in a number. If you want to check the number of digits as per the requirement you can view the sample I posted below. #include <iostream> using namespace std; int main() { int n = 0; while( cin >> n ) { if( n > 99999 && n < 1000000 ) { int digit_sum = n % 10, tmp = n; while( tmp /= 10 ) { digit_sum += tmp % 10; } cout << n << "\t - Sum of digit = " << digit_sum; } else { cout << n << "\t - Enter 6 digit integer only."; } cout<< endl; } return 0; } Hth, cmiiw
3rd Dec 2017, 9:51 AM
Ipang
+ 1
Sir i want it to get the ans by if loop
3rd Dec 2017, 9:56 AM
alikhan124
alikhan124 - avatar
0
If statement
3rd Dec 2017, 1:19 PM
alikhan124
alikhan124 - avatar