C++ Program to Find Last Digit of a Number

Howdy readers, today you will learn how to write a C++ program to find the last digit of a number.

This tutorial prompts the user to enter an integer, then it finds the last digit of the entered integer using the (%) modulus operator.

The last digit of a number is computed using the following approaches:

  1. Using (%) Modulus Operator
  2. Using Function

So, without any delay, let’s begin this tutorial.

C++ Program to Find Last Digit of a Number

C++ Program

// C++ Program to Find the Last Digit of a Number
#include <iostream>
using namespace std;

int main(){
    int num, lastdigit;
    
    // Taking input
    cout << "Enter a number: ";
    cin >> num;
    
    // Finding last digit
    lastdigit = num % 10;
    
    // Display last digit
    cout << "The last digit of " << num << " is: " << lastdigit;
    
    return 0;
}

Output

Enter a number: 287
The last digit of 287 is: 7
C++ Program to Find Last Digit of a Number

Explanation

    int num, lastdigit;

We have declared two int data type variables named num and lastdigit.

    cout << "Enter a number: ";
    cin >> num;

Then, the input is taken from the user using the cin statement. The entered value gets stored in the num variable.

    lastdigit = num % 10;

The last digit of the entered number is found using the (%) modulus operator. The (%) modulus operator returns the remainder after division.

If we divide any number by 10, then the remainder obtained is automatically the last number. If the number is completely divisible by 10 and leaves 0 as the remainder, then it means 0 is the last digit of the entered integer.

The last digit obtained from the above step gets stored in the lastdigit named variable.

    cout << "Last digit of " << num << " is: " << lastdigit;

Finally, the last digit of the entered number is printed on the screen using the cout statement.

C++ Program to Find Last Digit of a Number Using Function

C++ Program

// C++ Program to Find the Last Digit of a Number Using Function
#include <iostream>
using namespace std;

// User-defined function
int Digit(int n){
    return n % 10;
}

int main(){
    int num, lastdigit;
    
    // Taking input
    cout << "Enter a number: ";
    cin >> num;
    
    // Calling out function
    lastdigit = Digit(num);
    
    // Display output
    cout << "The last digit of the entered number is: " << lastdigit;
    
    return 0;
}

Output

Enter a number: 1024
The last digit of the entered number is: 4
C++ Program to Find Last Digit of a Number Using Function

Explanation

int Digit(int n){
    return n % 10;
}

We have declared and defined a function named Digit which passes a number as an argument and computes the last digit of the passed argument.

    lastdigit = Digit(num);

Then, this user-defined function is called out in the main function. This gives us the value of the last digit. The returned value gets stored in the lastdigit variable.

    cout << "Last digit of the entered number is: " << lastdigit;

After that, the last digit of the entered number is printed on the screen.

Conclusion

Today you learned how to write a C++ program to find the last digit of a number.

If you have any queries related to the program, comment down your questions in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment