C++ Program to find sum of first and last digit of a number


In this program, You will learn how to find sum of first and last digit of a number in C++.


Number is : 1234

Sum is : 1 + 4 = 5


Example: How to find sum of first and last digit of a number in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int a, r, fd, ld, sum, rev = 0;

    cout << "Enter a Number :";
    cin>>a;

    ld = a % 10;

    while (a > 0) {
        r = a % 10;
        rev = rev * 10 + r;
        a = a / 10;
    }

    fd = rev % 10;
    sum = ld + fd;

    cout << "\nSum Of First & Last digit :" << sum;

    return 0;
}

Output:

Enter a Number :2464
Sum Of First & Last digit :6