First and Last Digit Codechef Solution

Hello Programmers In this post, you will know how to solve the First and Last Digit Codechef Solution.

Ezoicreport this adFirst and Last Digit Codechef Solution
First and Last Digit Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

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 Solutions 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 Solutions 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';
    }
    }

Ezoicreport this adFirst and Last Digit CodeChef Solutions 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);
        }
    }
}
Ezoicreport this ad

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

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Turbo Sort Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad