Python Addition of total number by providing user input

Addition of Two Numbers in Python

While performing addition in python then we can do two ways:The first way is just to assign values to the variable and then add that number and assign to the same variable or third variable.

Example:

variable1 = 10
variable2 = 20
variable3 = variable1 + variable2
print('Total output is: ',variable3)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Total output is:  30

Process finished with exit code 0
The second way is that take a user input and assign in some variable and then add that two variable and assign in any variable. But by default python take string input so we have to convert it into an integer.

Example:

variable1 = input('Enter your first number:') # By defult input is srint

varible1 = int(variable1) #String conveted into integer.

variable2 = input('Enter your second number:')
varible1 = int(variable2) #String conveted into integer.

variable3 = variable1 + variable2

print('Total output is: ',variable3)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Enter your first number:10
Enter your second number:20
Total output is:  30

Process finished with exit code 0
How to convert each character of String as integer and then Sum it
user will enter the value in the string but we are converting each string into an integer and then adding all

Example:

num=input("enter digit to sum it: ")

total=int(num[0])+int(num[1])+int(num[2])+int(num[3])

print("total sum is:",total)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter digit to sum it: 1234
total sum is: 10

Process finished with exit code 0

How to convert each character of String as integer and then Sum it using while loop

Here also user will enter string value as input than in while loop find the length of input and then converting input into integer then performing an addition operation 

Example:

number = input("enter number:")
totals=0
i=0
while i < len(number):
     totals+=int(number[i])
     i+=1
print("Total sum is:",totals)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter number:1234
Total sum is: 10

Process finished with exit code 0

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment