Solution for Codechef FLOW004 | First and Last Digit

Hello Programmers,

The solution for codechef First and Last Digit problem is given below.

Problem Link:- https://www.codechef.com/problems/FLOW004

/*
*   Author:- Rahul Malhotra
*   Source:- Programming Vidya
*   Description:- Solution for Codechef First and Last Digit problem
*   Problem Link:- https://www.codechef.com/problems/FLOW004
*   Website:- www.programmingvidya.com
*/

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    // * Initializing variables
    int t, n, sum;

    // * Accepting the number of test cases
    cin>>t;

    // * Running each test case one by one
    while(t--)
    {
        // * Accepting the integer n
        cin>>n;

        // * Assigning the last digit
    	int last = n%10;

        // * Calculating the number of digits
    	int digits = (int) log10(n);

        // * Assigning the first digit
        int first = (int)(n / pow(10, digits));

        // * Calculating the sum of first and last digit
        sum = first + last;

        // * Displaying the value of sum
        cout<<sum<<endl;
    }
    return 0;
}

Happy Coding..!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s