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
forloop andrange()functions = 0to store the sum of all numbersinput()to take input from a userint()constructor and save it to variablenntimes using for loop and range() functioni) to variablesprint()function to display the variableson screenApproach 2: Use the built-in function sum(). The sum() function calculates the addition of numbers in the list or range
Solution1:Using
forloop andrange()functionSolution2:Using the built-in function
need an explanation for this answer? contact us directly to get an explanation for this answersum()