Dremendo Tag Line

Sum of first and last digit in C++

cin - Question 5

In this question, we will see how to input a two digit number in C++ programming using the cin statement and find the sum of its first and last digit. To know more about cin statement click on the cin statement lesson.

Q5) Write a program in C++ to input a two digit number and find the sum of its first and last digit.

Program

#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
    int n,fd,ld,sum;
    cout<<"Enter a two digit number\n";
    cin>>n;
    fd=n/10;
    ld=n%10;
    sum=fd+ld;
    cout<<"First digit="<<fd<<endl;
    cout<<"Last digit="<<ld<<endl;
    cout<<"Sum of first and last digit="<<sum;
    return 0;
}

Output

Enter a two digit number
56
First digit=5
Last digit=6
Sum of first and last digit=11
video-poster