Header Ads Widget

First and Last Digit | Codechef solution

  Problem:-

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

Input

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

Output

For each test case, display the sum of first and last digits of N in a new line.

Constraints

  •  T  1000
  •  N  1000000

Example

Input
3 
1234
124894
242323

Output
5
5
5

Code:-

#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n,digit=1,sum=0;
cin>>n;
int n1=n;
int r=n%10;
n=n/10;
sum+=r;
while(n!=0)
{
r=n%10;
n=n/10;
digit+=1;
}
int f=pow(10,digit-1);
r=n1/f;
sum+=r;
cout<<sum<<endl;
}
 
    return 0;
}

Recommended Post:

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

Key points:-

 MCQs:-