Wednesday, March 29, 2017

First and Last Digit

If Give an integer N, Write a program to obtain the sum of the first and last digit of this number.

Input
The first line contains an integer T, total number of test cases. Then follow T lines, each line contains an integer N.

Output
Display the sum of first and last digit of N.

Constraints
1 ≤ T ≤ 1000
1 ≤ N ≤ 1000000


#include <stdio.h>
int main()
{   int t,i,n,x;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {   scanf("%d",&n);
        if(n<10)
            printf("%d",n);
        else
        {   x=n%10;
            while(n>=10)
                n=n/10;
            x+=n;
            printf("%d\n",x);
        }
    }
    return 0;
}

No comments:

Post a Comment