first and last digit codechef solution

Sure! Here is a response to your query.

#include <iostream>
using namespace std;

int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int lastDigit = n % 10;
        while(n >= 10) {
            n /= 10;
        }
        int firstDigit = n;
        int sum = firstDigit + lastDigit;
        cout << sum << endl;
    }
    return 0;
}

Explanation:

  • We start by including the necessary header files and defining the standard namespace.
  • Inside the main function, we read the number of test cases t from the standard input.
  • We then enter a while loop that runs t times.
  • For each test case, we read the input number n.
  • We extract the last digit of n using the modulo operator %.
  • Next, we enter another while loop to find the first digit of n by repeatedly dividing it by 10 until it becomes less than 10.
  • We calculate the sum of the first and last digits and output the result for each test case.

I hope this helps!