Write a program to accept a number from a user and calculate the sum of all numbers from 1 to a given number
Expected Output:
Enter a value: 5
Sum of all values: 15
With both the statements and the output in python
Answers
Answered by
6
Programme:-
n = int(input("Enter a number: "))
s = 0
for i in range(n + 1):
s += i
print("The sum of all numbers from 1 to the given number is: ", s)
Logic used:-
- Ask the user to enter a number. Read: n.
- Run a loop from 0 to the given number.
- Calculate the sum of all numbers from 1 to the given number.
- Assign the result.
More Information:-
Python is a powerful high-level, object-oriented programming language. It was created by Guido van Rossum. It was launched in 1991. It can connect to the database system, it can also read and modify files. Python works on different platforms like Raspberry Pi, Linux, Mac, Windows and many more platforms.
Attachments:
Answered by
0
Answer:
n = int(input("Enter a number: "))
s = 0
for i in range(n + 1):
s += i
print("The sum of all numbers from 1 to the given number is: ", s)
Explanation:
Similar questions