Home  >  Article  >  Backend Development  >  Python program to find sum of first and last numbers

Python program to find sum of first and last numbers

王林
王林forward
2023-09-13 11:17:081139browse

Python program to find sum of first and last numbers

In this article, the given task is to add the first and last digits of an integer. Now integers can be very small or very large. Therefore, these plans will be divided into two parts. First, we need to find how big this integer is and then get the first number from it. The second part is to get the last number from the given integer, this can be easily done by dividing the number by ten and finding the remainder. In this Python article, we show how to add the first and last digits of an integer using four different examples.

In the first example, use repeated division by 10 to get the number of digits in an integer. In Example 2, math.log10() is used to get the number of digits of an integer. In Example 3, the integer is converted to a string to find its length; in Example 4, the integer is first converted to a string and then the index values ​​0 and -1 are used to get the first and last numbers. Then add the first and last numbers to get the result.

Example 1: Find the number of numbers by using repeated division to find the sum of the first and last digits of an integer.

algorithm

Step 1 - Write a countDigits function to count the number of digits in an integer.

Step 2 - Use the repeated division method.

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

Step 4 - Get the last number by dividing by 10 and taking the remainder.

Step 5 - Add the first and last numbers.

Step 6 - Do this for numbers of different lengths given in the array.

Step 7 - Print the output.

The Chinese translation of

Code

is:

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 the number of digits in a number by using the math.log10 function to find the sum of the first and last digits of an integer.

algorithm

Step 1 - To count the number of digits in an integer, write the countDigits function.

Step 2 - Use the formula math.floor(math.log10(thenumber) 1) in this function.

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

Step 4 - Divide by 10 and get the remainder to get the last number.

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

Step 6 - Use an array with different integers to do this for numbers of different lengths.

Step 7 - Print the 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 the sum of the first and last digits of an integer by converting an int to str and using the len function to find the digits

algorithm

Step 1 - Write a countDigits function to count the number of digits in an integer.

Second step - Inside this function, for the count, first convert the int to str and then get its length.

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

Step Four - Get the last number by dividing by ten and getting the remainder.

Step 5 - Now add the first and last number.

Step 6 - Execute this method for all numbers given in the array.

Step 7 - Print the 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

Figure 3: Output of Example 3 in the CMD window

Example 4: Find the sum of the first and last digits of an integer by using a string index value to find the first and last digits

algorithm

Step 1 - First convert the integer to a string.

Step 2 - Get the first number using index 0 and convert it back to an integer.

Step 3 - Get the last digit using index -1 and convert it back to an integer.

Step 4 - Add the first and last numbers.

Step 5 - Do this for numbers of different lengths given in the array.

Step 6 - Print the calculated total.

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 

The numbers are specified and extracted from an array.

in conclusion

We have given here various methods to show how to add the first and last digits of an integer. Different integers of different lengths are written into an array. Then use different methods on these integers. The methods differ mainly in the method of finding the median of an integer or the method of finding the first and last numbers from it.

The above is the detailed content of Python program to find sum of first and last numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete