Obtain the sum of the first and last digits of this number.

Write a program to obtain the sum of the first and last digits of this number.

Solution:

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int t;
    cin>>t;
   
    for(int i=0; i<t; i++)
    {
        int n;
        cin>>n;
        if (n<10)
        {
            cout<<n<<endl;
        }
        else{
        int last = n%10;// get the last digit
   
        while(n>9)
        {
            n/=10;// get first digit
        }
           int sum= last+ n;
           cout<<sum<<endl;
        }
    }
    return 0;
}

Comments

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.