A special site for solving fun programming problems and challenges, interested in computer science, programming, basics, data structure and algorithms

First and Last Digit | CodeChef Solution - CodingBroz

First and Last Digit Problem Code: FLOW004
Submit

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
-------------------------------------------------------------


solution in cpp
#include<iostream>
using namespace std;
// Get first number
int first_number(int n)
{
	while (n > 9)
	{
		n /= 10;
	}
	return n;
}
// Get last number
int last_number(int n)
{
	return n %= 10;
}
int main()
{
	int t, n, sum;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		cin >> n;
		sum = 0;
		sum = first_number(n) + last_number(n);
		cout << sum << "\n";
	}
	return 0;
}

 

No comments:

Post a Comment