what is wrong in this program-
using namespace std;
int main() {
int T;
cin>>T;
while(T--){
int a,sum = 0;
cin>>a;
int n = a%10;
while(n>0){
sum = sum + n;
a = a/10;
n = a%10;
}
cout<<sum<<endl;
}
return 0;
}
Problem-
what is wrong in this program-
using namespace std;
int main() {
int T;
cin>>T;
while(T--){
int a,sum = 0;
cin>>a;
int n = a%10;
while(n>0){
sum = sum + n;
a = a/10;
n = a%10;
}
cout<<sum<<endl;
}
return 0;
}
Problem-
your code will not run for numbers that are completely divisible by 10, like 100. As per ur first statement i.e. n=a%10 will equals to 0 for such numbers and the condition n>0 will be false.
Fixed your solution, maybe you can find your mistakes.
#include <iostream>
using namespace std;
int main()
{
int T, n, a;
cin >> T;
while (T--)
{
int sum = 0;
cin >> a;
//int n = a % 10;
while (a > 0)
{
n = a % 10;
sum = sum + n;
a = a / 10;
}
cout << sum << endl;
}
return 0;
}
THANKS FOR YOUR HELP