How do you get the first and last digit of a number in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a number and to find first and last digit of a number.
    Examples: 
     

    Input : 12345 
    Output : First digit: 1
             last digit : 5
    
    Input : 98562
    Output : First digit: 9
             last digit : 2

    To find last digit of a number, we use modulo operator %. When modulo divided by 10 returns its last digit. 
    Suppose if n = 1234 
    then last Digit = n % 10 => 4 
    To finding first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
     

    Approach 1 (With loop):

    C++

    #include

    using namespace std;

    int firstDigit(int n)

    {

        while (n >= 10) 

            n /= 10;

        return n;

    }

    int lastDigit(int n)

    {

        return (n % 10);

    }

    int main()

    {

        int n = 98562;

        cout << firstDigit(n) << " " 

            << lastDigit(n) << endl;

        return 0;

    }

    Java

    import java.util.*;

    import java.lang.*;

    public class GfG{

        public static int firstDigit(int n)

        {

            while (n >= 10

                n /= 10;

            return n;

        }

        public static int lastDigit(int n)

        {

            return (n % 10);

        }

        public static void main(String argc[])

        {

            int n = 98562;

            System.out.println(firstDigit(n) + " "

            + lastDigit(n));

        }

    }

    Python3

    def firstDigit(n) :

        while n >= 10

            n = n / 10;

        return int(n)

    def lastDigit(n) :

        return (n % 10)

    n = 98562;

    print(firstDigit(n), end = " "

    print(lastDigit(n))

    C#

    using System;

    public class GfG{

        public static int firstDigit(int n)

        {

            while (n >= 10) 

                n /= 10;

            return n;

        }

        public static int lastDigit(int n)

        {

            return (n % 10);

        }

        public static void Main()

        {

            int n = 98562;

            Console.WriteLine(firstDigit(n) + " "

            + lastDigit(n));

        }

    }

    PHP

    function firstDigit($n)

    {

        while ($n >= 10) 

            $n /= 10;

        return (int)$n;

    }

    function lastDigit($n)

    {

        return ((int)$n % 10);

    }

    $n = 98562;

    echo firstDigit($n) . " " .

         lastDigit($n) . "\n";

    Javascript

            document.write(firstDigit(n) + " "

            + lastDigit(n));

    Time Complexity: O(log10n)
    Auxiliary Space: O(1) 

    Approach 2 (Without loop) 

    C++

    #include

    using namespace std;

    int firstDigit(int n)

    {

        int digits = (int)log10(n);

        n = (int)(n / pow(10, digits));

        return n;

    }

    int lastDigit(int n)

    {

        return (n % 10);

    }

    int main()

    {

        int n = 98562;

        cout << firstDigit(n) << " " 

             << lastDigit(n) << endl;

        return 0;

    }

    Java

    import java.math.*;

    class GFG {

        static int firstDigit(int n)

        {

            int digits = (int)(Math.log10(n));

            n = (int)(n / (int)(Math.pow(10, digits)));

            return n;

        }

        static int lastDigit(int n)

        {

            return (n % 10);

        }

        public static void main(String args[])

        {

            int n = 98562;

            System.out.println(firstDigit(n) +

                               " " + lastDigit(n));

        }

    }

    Python3

    import math

    def firstDigit(n) :

        digits = (int)(math.log10(n))

        n = (int)(n / pow(10, digits))

        return n;

    def lastDigit(n) :

        return (n % 10)

    n = 98562;

    print(firstDigit(n), end = " "

    print(lastDigit(n))

    C#

    using System;

    class GFG {

        static int firstDigit(int n)

        {

            int digits = (int)(Math.Log10(n));

            n = (int)(n / (int)(Math.Pow(10, digits)));

            return n;

        }

        static int lastDigit(int n)

        {

            return (n % 10);

        }

        public static void Main()

        {

            int n = 98562;

            Console.WriteLine(firstDigit(n) +

                            " " + lastDigit(n));

        }

    }

    PHP

    function firstDigit($n

        $digits = (int)log10($n); 

        $n = (int)($n / pow(10, $digits)); 

        return $n

    function lastDigit($n

        return ($n % 10); 

    $n = 98562; 

    echo firstDigit($n) , " ",

         lastDigit($n), "\n"

    ?>

    Javascript

    Time Complexity: O(1)
    Auxiliary Space: O(1)

    Important note: log10() is a mathematical function present in math.h header file. It returns log base 10 value of the passed parameter to log10() function. 


    How do I find the first and last digit of a number in Python?

    print("Enter a Number: ", end="") try: num = int(input()) count = 0 while num != 0: if count == 0: last = num % 10 count = count + 1 rem = num % 10 num = int(num / 10) print("\nFirst Digit (", rem, ") + Last Digit (", last, ") =", rem + last) except ValueError: print("\nInvalid Input!")

    How do you find the first digit and the last digit of a number?

    To find last digit of a number, we use modulo operator %. When modulo divided by 10 returns its last digit. To finding first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10.

    How do you get the last digit of a number in Python?

    How to Get the Last Digit of a Decimal in Python.
    pi = 3.141..
    last_digit = int(repr(pi)[-1]).
    print(f"The last digit of {pi} is {last_digit}").

    How do you get the first digit of a number in Python?

    To get the first digit, we can use the Python math log10() function. In the loop example, we divided by 10 until we got to a number between 0 and 10. By using the log10() function, we can find out exactly how many times we need to divide by 10 and then do the division directly.