Write a python program to accept a number from a user and calculate the sum of all numbers from 1 to a given number
belongs to collection: Python Loop Exercises
All Answers
total answers (1)
belongs to collection: Python Loop Exercises
total answers (1)
Hint:
Approach 1: Use
for
loop andrange()
functions = 0
to store the sum of all numbersinput()
to take input from a userint()
constructor and save it to variablen
n
times using for loop and range() functioni
) to variables
print()
function to display the variables
on screenApproach 2: Use the built-in function sum(). The sum() function calculates the addition of numbers in the list or range
Solution1:Using
for
loop andrange()
functionSolution2:Using the built-in function
need an explanation for this answer? contact us directly to get an explanation for this answersum()