Sum of last digits of two given 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].

    Sum of last digits of two given 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


    Last update on August 19 2022 21:51:44 (UTC/GMT +8 hours)

    Python Basic - 1: Exercise-33 with Solution

    Write a Python program to compute the digit number of sum of two given integers.

    Input:
    Each test case consists of two non-negative integers x and y which are separated by a space in a line.
    0 ≤ x, y ≤ 1,000,000

    Pictorial Presentation:

    Sum of last digits of two given numbers in python

    Sample Solution:

    Python Code:

    print("Input two integers(a b): ")
    a,b = map(int,input().split(" "))
    print("Number of digit of a and b.:")
    print(len(str(a+b)))
    

    Sample Output:

    Input two integers(a b): 
     5 7
    Number of digit of a and b.:
    2
    

    Flowchart:

    Sum of last digits of two given numbers in python

    Python Code Editor:

    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

    Previous: Write a python program to find heights of the top three building in descending order from eight given buildings.
    Next: Write a Python program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No".

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

    “program to add first and last digit of a number in python” Code Answer.
    n = int(input("Enter any number : ")).
    number = str(n).
    first = int(number[0]).
    last = int(number[-1]).
    print(f"sum of {first} and {last} is : ",sum).

    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 print the sum of the first and last elements in a list Python?

    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..

    How do you sum numbers in Python?

    sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.