First and Last Digit Codechef Solution

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

  • 1 <= T <= 1000
  • 1 <= N <= 1000000

Example

Input:

3
1234
124894
242323

Output:

5
5
5

First and Last Digit – CodeChef Solution in Python

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

First and Last Digit – CodeChef Solution in CPP

#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 in JAVA

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);
        }
    }
}

Disclaimer: The above Problem (First and Last Digit) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment