I need help for this problem

I got the problems of return the sum of last digit and first digit of number N . Can someone help me find where did i got the wrong idea pls ! This is my code (I use C++)

#include <iostream>

using namespace std;

long addTwoDigit(long num1){
    long sum = 0;
    sum += num1 % 10;
    for(long i = 10; i < num1; i*= 10){
        long test = num1/i;
        if(test < 10){
            sum += test % 10;
        }
    }

    return sum;
}

int main(){
    int t;
    cin >> t;
    while (t--){
        long num1;
        cin >> num1;
        long sum = addTwoDigit(num1);

        cout << sum << "\n";
    }

    return 0;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

What is the loop for?

That’s for the number of test case

If you add some couts, what are the values of test?

i got the exact value but when i submit this problem they told that this is the wrong answer

What is the problem prompt?

I meant the description of what this code is supposed to do, not a screenshot of the test suite.

https://www.codechef.com/problems/FLOW004 Here is the link of the problem

And what’s your output for the sample input?

The logic looks ok, but I’m tired enough that I might be missing something.

exacly like the sample output

Well, without any more specific information, I can’t really say anything. The logic looks ok and it looks like ok C++, but I’m not familiar with that platform.

1 Like