First and Last Digit | CodeChef Solution

Today we will be solving First and Last Digit CodeChef Problem in Python, Java and C++ whose code is FLOW004.

first-and-last-digit-codechef-solution-codebrosindia

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

  • 1 ≤ T ≤ 1000
  • 1 ≤ N ≤ 1000000

Example

Input

3 
1234
124894
242323

Output

5
5
5

Solution – First and Last Digit CodeChef Solution

Python 3

#Solution Provided by CodingBroz 
T = int(input())
while T > 0:
    n = (input())
    reverse = n[::-1]
    reverse = int(reverse)
    n = int(n)
    first_digit = reverse % 10
    last_digit = n % 10
    sum = first_digit + last_digit
    print(sum)
    T = T - 1

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int T=sc.nextInt();
		while(T-->0){
		    int n=sc.nextInt();
		    int [] a=new int[100];
		    int i=0;
		    while(n!=0){
		        a[i]=n%10;
		        n=n/10;
		        i++;
		    }
		    int sum=a[0]+a[i-1];
		    System.out.println(sum);
		}
	}
}
First and Last Digit CodeChef Solution

C++

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


int main() {

	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	int t ;
	cin >> t;
	while(t--){
	int n,len=0;
	cin >> n;
	int temp=n;
	while(temp/=10){
	      len++;
	}
	int last_digit = n%10;
	int first_digit = n/pow(10,len);
	cout << last_digit+first_digit <<'\n';
	}

	}
First and Last Digit CodeChef Solution

Disclaimer: The above problem (First and Last Digit : FLOW004) is generated by CodeChef but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning Purposes.

Broz Who Code

CodingBroz

Leave a Comment

Your email address will not be published. Required fields are marked *