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

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


while(condition){
     //statement
     //increment/decrement
}

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

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

#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 << "Sum of first & last digit:" << sum;

    return 0;
}

Output:

Enter a number:324
Sum of first & last digit:7