C++ program to find the first and the last digits of a number

4 ways in C++ to find the first and the last digits of a number:

In this C++ tutorial, we will learn how to find the first and the last digits of a user-given number. For example, if the number is 12459, it will print 9 as the last digit and 1 as the first digit.

Our program will take the number as input from the user and print out the first and the last digits.

Method 1: C++ program to find the first and the last digits of a number with a loop and modulo operator:

We can use the modulo operator, % and a loop to find the last and the first number. Below is the complete C++ program to find the first and the last digits of a number:

#include <iostream>
using namespace std;

int main()
{
    int number;
    cout << "Enter a number : " << endl;
    cin >> number;

    cout << "Last digit is : " << number % 10 << endl;

    while (number >= 10)
    {
        number = number / 10;
    }

    cout << "First digit is : " << number << endl;
    return 0;
}

Download this program on Github

Explanation :

  • The program is using an integer variable number to hold the user input number.
  • It is using the cout syntax to enter a message on the console. The cin syntax is used to read the user input number and assign it to the number variable.
  • The second cout syntax prints the last digit of the number. The modulo operator % is used to find the remainder of a division operation. The operation x % y returns the remainder if x is divided by y. Similarly, the operation x % 10 gives the last digit of that number i.e. the remainder if we divide x by 10.
  • To find the first digit, we are removing all digits of the number one by one starting from the rightmost digit. We are dividing the number by 10 in the while loop. It will remove the rightmost digit on each step. The loop will run until the current value of the number becomes less than 10 which is the first digit of the number.

Following are some sample outputs of the above program :

Enter a number :
129897
Last digit is : 7
First digit is : 1

Enter a number :
98976790
Last digit is : 0
First digit is : 9

Enter a number :
99999999
Last digit is : 9
First digit is : 9

c-plus-plus-first-last-digit

Method 2: Get the first digit of a number with a for loop in C++:

Instead of using a while loop, we can also use a for loop to get the last digits of a number. Similar to the previous example, we can use a for loop to remove the digits of a number to get the first digit of the number. We will have to use an empty for loop as shown in the below program:

#include <iostream>
using namespace std;

int main()
{
    int number;
    cout << "Enter a number : " << endl;
    cin >> number;

    cout << "Last digit is : " << number % 10 << endl;

    for (; number >= 10; number /= 10)
    {
    }

    cout << "First digit is : " << number << endl;
    return 0;
}

Download this program on Github

This program is using an empty for loop. On each step, it is dividing the number by 10 to remove the rightmost digit of the number. The loop will stop once the value of the number variable is smaller than 10. It will print similar output.

Method 3: C++ program to find the first and the last digits of a number by converting it to a string:

We can also convert the number to a string, pick the first and the last characters of the string and convert these to integers to get the first and the last digits of the number. The following example finds the first and the last digits of a number by converting it to a string:

#include <iostream>
using namespace std;

int main()
{
    int number;
    cout << "Enter a number : " << endl;
    cin >> number;

    string strNumber = to_string(number);
    int firstDigit = strNumber.front() - '0';
    int lastDigit = strNumber.back() - '0';

    cout << "First digit is : " << firstDigit << endl;
    cout << "Last digit is : " << lastDigit << endl;
    return 0;
}

Download this program on Github

  • The to_string method is used to convert an integer to a string.
  • The front() and back() methods are used to get the ASCII values of the first and the last character of the string. We need to subtract the character '0' to get the integer value corresponding to that ASCII.

It will print similar output:

Enter a number : 
1234
First digit is : 1
Last digit is : 4

Method 4: Find the first digit by calculating the total number of digits of the number:

If we divide the number by 10^(n - 1), where n is the total number of digits of the number and convert it to an integer, it will be the first digit of that number. For example, if the number is 1234, we can divide it by 10^3 or 1000 to get 1.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int number;
    cout << "Enter a number : " << endl;
    cin >> number;

    cout << "Last digit is : " << number % 10 << endl;

    int d = (int)log10(number);
    int firstDigit = (int)(number / pow(10, d));

    cout << "First digit is : " << firstDigit << endl;
    return 0;
}

Download this program on Github

  • The statement (int)log10(number) gives the value of total digits - 1 of the number. This value is assigned to the variable d.
  • To calculate the first digit of the number, we are dividing the number by 10^d. The pow() method is used to calculate the power.
  • The last digit is calculated with the modulo operator.

It will print similar results:

Enter a number : 
12345
Last digit is : 5
First digit is : 1

Similar tutorials :