Addition of number using for loop and providing user input data in python

Addition of Number using for loop

In this program addition of numbers using for loop, we will take the user input value and we will take a range from 1 to input_num + 1. Using for loop, we will sum all the values.

num=int(input("enter number:"))
total=0
for i in range(1,num+1):
total+=i
i+=1
print("total sum is:",total)

Sum of all input number in Python

n=input("enter number: ").split(',')

total=0
for i in range(len(n)):
total+=int(n[i])
i+=1
print(total)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter number: 1,2,3,4,5
15

Process finished with exit code 0

A simple way for Addition by using def in Python

def add():
return "sum of these two number is:",11+99
print(add())

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter number: saa
('sum of these two number is:', 110)

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