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 ofCode
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Standby is a lock screen mode that activates when the iPhone is plugged into the charger and oriented in horizontal (or landscape) orientation. It consists of three different screens, one of which is displayed full screen time. Read on to learn how to change the style of your clock. StandBy's third screen displays times and dates in various themes that you can swipe vertically. Some themes also display additional information, such as temperature or next alarm. If you hold down any clock, you can switch between different themes, including Digital, Analog, World, Solar, and Floating. Float displays the time in large bubble numbers in customizable colors, Solar has a more standard font with a sun flare design in different colors, and World displays the world by highlighting

The ability to generate random numbers or alphanumeric strings comes in handy in many situations. You can use it to spawn enemies or food at different locations in the game. You can also use it to suggest random passwords to users or create filenames to save files. I wrote a tutorial on how to generate random alphanumeric strings in PHP. I said at the beginning of this post that few events are truly random, and the same applies to random number or string generation. In this tutorial, I'll show you how to generate a pseudo-random alphanumeric string in JavaScript. Generating Random Numbers in JavaScript Let’s start by generating random numbers. The first method that comes to mind is Math.random(), which returns a float

Which ASUS motherboard should be paired with the R55600? The ASUS ROGStrixB550-FGaming motherboard is an excellent choice. It is perfectly compatible with Ryzen55600X processor and provides excellent performance and features. This motherboard has a reliable power supply system, can support overclocking, and provides a wealth of expansion slots and ports to meet daily use and gaming needs. ROGStrixB550-FGaming is also equipped with high-quality audio solutions, fast network connections and reliable heat dissipation design to ensure that the system remains efficient and stable. In addition, this motherboard adopts a gorgeous ROG style and is equipped with gorgeous RGB lighting effects, adding visual enjoyment to your computer. All in all, ASUS ROGStri

Representing numbers as output is an interesting and important task when writing a program in any language. For integer types (data of type short, long, or medium), it is easy to represent numbers as output. For floating point numbers (float or double type), sometimes we need to round them to a specific number of decimal places. For example, if we want to represent 52.24568 as three decimal places, some preprocessing is required. In this article, we will introduce several techniques to represent floating point numbers to a specific number of decimal places by rounding. Among the different approaches, it is important to use a C-like format string, use the precision argument, and use the round() function from the math library. Let’s look at them one by one. with

Which one is better, Celeron g4900 or i36100? When it comes to the two processors Celeron G4900 and I36100, there is no doubt that the performance of I36100 is superior. Celeron processors are generally considered low-end processors and are primarily used in budget laptops. The I3 processor is mainly used for high-end processors, and its performance is very good. Whether you are playing games or watching videos, you will not experience any lagging when using the I3 processor. Therefore, if possible, try to buy Intel I-series processors, especially for desktop computers, so that you can enjoy the fun of the online world. How is the performance of the Celeron G4900T? From a performance perspective, the Pentium G4900T performs well in terms of frequency. Compared with the previous version, the CPU performance is

Linked lists use dynamic memory allocation, i.e. they grow and shrink accordingly. They are defined as collections of nodes. Here, a node has two parts, data and links. The representation of data, links and linked lists is as follows - Types of linked lists There are four types of linked lists, as follows: - Single linked list/Singly linked list Double/Doubly linked list Circular single linked list Circular double linked list We use the recursive method to find the length of the linked list The logic is -intlength(node *temp){ if(temp==NULL) returnl; else{&n

We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,

Here we will see how to get the number A raised to the power B using a bash script. The logic is simple. We have to use the "**" operator or the power operator to do this. Let us see the following program to understand this concept clearly. Example#!/bin/bash#GNUbashScripta=5b=6echo "$(($a**$b))" output 15625
