In this post, we will learn** how to find the first and last digit of a number using C++** Programming language.

Suppose the user enters a number** 5468**, then it’s first digit is **5** and last digit **8**. We will find the same in our program using simple arithmetic operators.

So, without further ado, let’s begin this tutorial.

**C++ Program to Find First and Last Digit of a Number**

// C++ Program to Find First and Last Digit of a Number #include <iostream> using namespace std; int main(){ int num, last; // Asking for input cout << "Enter a number: "; cin >> num; // Last Digit last = num % 10; // First digit while (num > 10){ num = num / 10; } // Displaying output cout << "The first digit of entered number is: " << num << endl; cout << "The last digit of entered number is: " << last << endl; return 0; }

**Output**

```
Enter a number: 12345
The first digit of entered number is: 1
The last digit of entered number is: 5
```

**How Does This Program Work ?**

int num, last;

In this program, we have declared** two int data type** variables named** num **and** last**.

// Asking for input cout << "Enter a number: "; cin >> num;

Then, the user is asked to enter a number.

// Last Digit last = num % 10;

We find the last digit of the entered number using the modulus **(%) **operator. Modulus operators return remainder after division.

// First digit while (num > 10){ num = num / 10; }

We found the first digit of the entered number using a simple** while **loop. Dividing the entered number by **10 **will remove all the digits one by one from right to left.

When the value of** num** is less than** 10**, the loop terminates leaving the value of **num **a single integer which is also the** first digit** of the number.

// Displaying output cout << "The first digit of entered number is: " << num << endl; cout << "The last digit of entered number is: " << last << endl;

Finally, the first and last digit of the entered number is displayed on the screen using the** cout** statement.

**Conclusion**

I hope after going through this post, you understand** how to find the first and last digit of a number using C++** Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.

**Also Read:**

NisaiftikharExplain again how you find the last digit