0
Write and run a program that reads a six digit integer and prints the sum of its six digits?
3 Respostas
+ 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
+ 1
Sir i want it to get the ans by if loop
0
If statement