My code says runtime error,could someone help me. Question-FLOW004

My code (in cpp):
#include
#include
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
    long int n;
    int sum=0;
    cin>>n;
    int first=0,second=0;
    first=n%10;
    n=n/10;
    for(;n<=9;)
    {
        second=n%10;
    }
    sum=first+second;
    cout<<sum;
    
}
return 0;

}

your loop does not change the value of n

Also, your condition to break the for loop is wrong…

it should be (n>0) instead of (n<=9)

Consider test case 143

Your code gives ans 3 while correct answer is 4

Give this problem a try…

Also, you forgot to add newline character in the cout statement…

If still unable to understand your error, have a look at this


[1].

Please Accept the Answer if you find this helpful.... :)

Feel free to ask anything...


  [1]: https://www.codechef.com/viewsolution/15460037

Here’s another approach:

Take the input number as a string and then find sum of 0th element and length-1 element minus 2*48.
Here’s the code for that: CodeChef: Practical coding for everyone.

Mate, don’t go on awarding points like this…

As i have heard, this reduces your karma…

Accepting the answer is better :smiley: