How do you add last digit of two numbers in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digit of the given number N.

    Examples:

    Input: N = 1247
    Output: 8
    
    Explanation: First digit is 1 and Last digit is 7. 
    So, addition of these two (1 + 7) is equal to 8.
    Input: N = 73
    Output: 10

    Method 1: String implementation

    • Take input in the form of String or typecast given input in String.
    • Now pick the 0th index of the String and typecast it into Integer and store it in a variable.
    • The same thing with the -1st index and also store in another variable.
    • Now add these two variables and
    • print them as an Output.

    Note:  We can access the first element of String using string[0] and the last element of String using string[-1].

    How do you add last digit of two numbers in python?

    String Representation


    Python3

    number = 1247

    number = str(number)

    first_digit = int(number[0])

    last_digit = int(number[-1])

    addition = first_digit + last_digit

    print('Addition of first and last digit of the number is',

          addition)

    Output:

    Addition of first and last digit of the number is 8

    Time complexity: O(1) since performing constant operations

    Auxiliary Space: O(1) since using constant space for variables

    Method 2: Solve it using an integer

    • We have given a positive Integer.
    • After dividing by 10, store the remainder in a result variable.
    • Continue the loop until the number becomes less than 9.
    • Each time in the loop, divide the number by 10(integer division).
    • After the end of the loop.
    • Add the number in the result variable.
    • Display the result variable as the output.

    Note: Whenever we divide any number with 10, we get the last digit as the remainder. If we divide any number with 100, we get the last two-digit as the remainder.

    Python3

    number = 1247

    res = number % 10

    while number > 9:

        number = number // 10

    res += number

    print('Addition of first and last digit of number is', res)

    Output:

    Addition of first and last digit of the number is 8

    Time Complexity: O(n), where n is how many digits are there in the given number.

    Auxiliary Space: O(1) since using constant space for variables


Python program to get an number n from user and display the last digit of n

Sample Input 1:

623

Sample Output 1:

3

Sample Input 2:

280

Sample Output 2:

0

Program or Solution

 num=int(input("enter the number:")) ldigit=num%10 print("last digit: {}".format(ldigit)) 

Program Explanation

Get a number num (using input() method) Seprate the last digit of number num by "digit = num%10".

(% by 10 always gives last digit of a number) print the digit (using print() method)


This Python program calculates sum of first and last digit of a given number.


In this program, we first read number from user. Then we reverse it using [::-1]. After reversing, we convert both number & reverse to integer using int() function. first_digit is obtained by reverse%10 and last_digit is obtained by number%10. Finally we calculate sum of first & last digit to get final result.

Python Source Code: Sum of First & Last Digit

 # Sum of first & last digit # Reading number number = input("Enter number: ") # Reversing number reverse = number[::-1] # Converting number and its reverse to integer number = int(number) reverse = int(reverse) # Finding first digit first_digit = reverse % 10 # Finding last digit last_digit = number % 10 # Finding sum total_sum = first_digit + last_digit # Displaying sum print("Sum of first & last digit of %d is %d" %(number, total_sum)) 

Output

Enter number: 99975
Sum of first & last digit of 99975 is 14

How do you take 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 sum a digit in Python?

The sum() method is used to compute the sum of digits of a number in python in a list. Convert the number to a string using str(), then strip the string and convert it to a list of numbers with the strip() and map() methods, respectively. Then, compute the total using the sum() method.

How do I get the last 3 digits of a number in Python?

Use the modulo operator to get the last 3 digits of a number, e.g. last_three = number % 1000 . The modulo operator will return the last 3 digits of the number by calculating the remainder of dividing the number by 1000. Copied!

How do you find the last two digits of a number?

Last two digits of a number is basically the tens place and units place digit of that number. So given a number say 1439, the last two digits of this number are 3 and 9, which is pretty straight forward.

Tải thêm tài liệu liên quan đến bài viết How do you add last digit of two numbers in python?