Why it is wrong answer? because my program is correct

what is wrong in this program-

#include

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.

@shreyash_49 instead of while(n>0) , while(a>0) may help you .

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