samedi 25 janvier 2020

How to calculate the sum of first and last digit of a number? What's wrong in my code? [closed]

I want to write a program to calculate the sum of the first and last digit of a number.

For example N = 12345 then sum = 6. constraint on number is : 1 <= N <= 1000000.

#include <iostream>

using namespace std;

int main()
{
    int T,N;
    cin >> T;
    for(int i = 0; i < T; i++){
        cin >> N;
        int digit = N % 10;
        while ((N / 10) != 0){
            N = N/10;
        }
        cout << digit + N;
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire