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
Post a Comment