Python Program to Find Sum of First and Last Digit


In this article, the given task is to add the first and the last digit of an integer. Now the integer can be very small or it can be a big number. So, these programs will have two parts. First, we need to find how big the integer is and then get the first digit from it. The second part will be to get the last number from the given integer which can be done easily by dividing the number by ten and finding the remainder. In this Python article, using four different examples, the approaches to adding the first and the last digits of an integer are given.

In the first example, the repeatedly dividing by 10 method is used to get the count of digits in an integer. In example 2, the math.log10() is used to get the number of digits in an integer. In example 3, the integer is converted to a string to find its length and in example 4, the integer is first converted into a string and then the index values 0 and -1 are used to fetch the first and the last number. These first and last numbers are then added together to get the result.

Example 1: Find Sum of First and Last Digits of an Integer by using repeated division for finding the count of digits.

Algorithm

Step 1 − Write a countDigits function to count the digits of an integer.

Step 2 − Use the repeated division method for this.

Step 3 − Now divide the integer by 10**count to fetch the first number.

Step 4 − Get the last number by dividing by 10 and getting the remainder.

Step 5 − Add the first and last number.

Step 6 − Do this for different lengths of numbers given in an array.

Step 7 − Print the output.

Code

listofnumbers =[881234,954321, 7178952, 20033, 459, 20069]
import math
#define function
def countDigits(thenumber):
   count=0
   while thenumber != 0:
      thenumber //= 10
      count += 1
   return count

#Use for loop
for item in listofnumbers:
   c=countDigits(item)

   firstnum=math.floor(item/10**(c-1))
   lastnum=item%10
   total=firstnum+lastnum

   print("\nThe Given number is: " , item)
   print("The first digit is ", firstnum)
   print("The last digit is ", lastnum)
   print("The sum of first and the last digit is " , total)

Output - Example 1

Run the python file in the Command window

Open the cmd window. Check the output in cmd window.

The Given number is:  881234
The first digit is  8
The last digit is  4
The sum of first and the last digit is  12

The Given number is:  954321
The first digit is  9
The last digit is  1
The sum of first and the last digit is  10

The Given number is:  7178952
The first digit is  7
The last digit is  2
The sum of first and the last digit is  9

The Given number is:  20033
The first digit is  2
The last digit is  3
The sum of first and the last digit is  5

The Given number is:  459
The first digit is  4
The last digit is  9
The sum of first and the last digit is  13

The Given number is:  20069
The first digit is  2
The last digit is  9
The sum of first and the last digit is  11 

Example 2: Find Sum of First and Last Digit of an Integer by using math.log10 function for finding the count of digits.

Algorithm

Step 1 − To count the digits of an integer, write a countDigits function.

Step 2 − In this function use the formula math.floor(math.log10(thenumber) + 1).

Step 3 − Now divide the integer by 10**count to fetch the first number

Step 4 − Get the last number by divide by 10 and getting the remainder.

Step 5 − To get the sum, add the first and last number.

Step 6 − Use an array with different integers to do this for different length of numbers.

Step 7 − Print the output sum.

listofnumbers =[1234,54321, 678952, 200, 45, 10069]
#Import the required module
import math
#define function
def countDigits(thenumber):
   return math.floor(math.log10(thenumber) + 1)
#Use for loop to iterate item    
for item in listofnumbers:
   c=countDigits(item)
   firstnum=math.floor(item/10**(c-1))
   lastnum=item%10
   total=firstnum+lastnum
    
   print("\nThe Given number is: " , item)
   print("The first digit is ", firstnum)
   print("The last digit is ", lastnum)
   print("The sum of first and the last digit is " , total) 

Output - Example 2

Run the python file in the Command window

Open the cmd window. Check the output in cmd window.

The Given number is:  1234
The first digit is  1
The last digit is  4
The sum of first and the last digit is  5

The Given number is:  54321
The first digit is  5
The last digit is  1
The sum of first and the last digit is  6

The Given number is:  678952
The first digit is  6
The last digit is  2
The sum of first and the last digit is  8

The Given number is:  200
The first digit is  2
The last digit is  0
The sum of first and the last digit is  2

The Given number is:  45
The first digit is  4
The last digit is  5
The sum of first and the last digit is  9

The Given number is:  10069
The first digit is  1
The last digit is  9
The sum of first and the last digit is  10

Example 3: Find Sum of First and Last Digit of an Integer by converting int to str and using len function for finding the count of digits

Algorithm

Step 1 − Write a countDigits function to count the digits of an integer.

Step 2 − Inside this function, for the count, first convert int to str and then get its length.

Step 3 − Now divide the integer by 10**count to get the first number.

Step 4 − Fetch the last number by dividing by ten and then get the remainder.

Step 5 − The first and last numbers are now added.

Step 6 − Do this method for all numbers given in an array.

Step 7 − Print the output sum.

listofnumbers =[11234,554321, 6789521, 2004, 3455, 60069]
import math
def countDigits(thenumber):
   snum=str(thenumber)
   l=len(snum)
   return l
    
for item in listofnumbers:
   c=countDigits(item)
   firstnum=math.floor(item/10**(c-1))
   lastnum=item%10
   total=firstnum+lastnum
   print("\nThe Given number is: " , item)
   print("The first digit is ", firstnum)
   print("The last digit is ", lastnum)
   print("The sum of first and the last digit is " , total) 

Output - Example 3

Run the python file in the Command window

Open the cmd window. Check the output in cmd window.

The Given number is:  11234
The first digit is  1
The last digit is  4
The sum of first and the last digit is  5

The Given number is:  554321
The first digit is  5
The last digit is  1
The sum of first and the last digit is  6

The Given number is:  6789521
The first digit is  6
The last digit is  1
The sum of first and the last digit is  7

The Given number is:  2004
The first digit is  2
The last digit is  4
The sum of first and the last digit is  6

The Given number is:  3455
The first digit is  3
The last digit is  5
The sum of first and the last digit is  8

The Given number is:  60069
The first digit is  6
The last digit is  9
The sum of first and the last digit is  15

Fig 3: Output in CMD window for Example3

Example 4: Find Sum of First and Last Digit of an Integer by using string index values for finding the first and the last digits

Algorithm

Step 1 − First convert the integer into a string.

Step 2 − Use index 0 to fetch the first digit and then convert it back to an integer.

Step 3 − Use index -1 to fetch the last digit and then convert it back to an integer.

Step 4 − Add the first and last number.

Step 5 − Do this for numbers of different lengths that are given in an array.

Step 6 − Print the total calculated.

listofnumbers =[12343,543210, 6789529, 9200, 45, 810069]
#Use for loop
for item in listofnumbers:
    snum=str(item)
    firstnum=int(snum[0])
    lastnum=int(snum[-1])
    total=firstnum+lastnum
    print("\nThe Given number is: " , item)
    print("The first digit is ", firstnum)
    print("The last digit is ", lastnum)
    print("The sum of first and the last digit is " , total)

Output - Example 4

Run the python file in the Command window

Open the cmd window. Check the output in cmd window.

The Given number is:  12343
The first digit is  1
The last digit is  3
The sum of first and the last digit is  4

The Given number is:  543210
The first digit is  5
The last digit is  0
The sum of first and the last digit is  5

The Given number is:  6789529
The first digit is  6
The last digit is  9
The sum of first and the last digit is  15

The Given number is:  9200
The first digit is  9
The last digit is  0
The sum of first and the last digit is  9

The Given number is:  45
The first digit is  4
The last digit is  5
The sum of first and the last digit is  9

The Given number is:  810069
The first digit is  8
The last digit is  9
The sum of first and the last digit is  17 

These numbers are specified and taken from an array.

Conclusion

We give here the various approaches to show how to add the first and the last number of an integer. Different integers of different lengths are written in an array. The different approaches are then used on these integers. The difference in these approaches is mainly in the method of finding the count of digits in an integer or in finding the first and the last digits from these.

Updated on: 10-Jul-2023

704 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements