🐍 Python Mga master bigyan niyo po ako ng real life problem na icocode gamit loops

BrigthWin

Forum Veteran
Yung medyo madali lang po sana mag papractice lang ako para sa exam namin pasabay nadin po ng possible output. Thank you so much ❤️
 
Sure! Here's a simple problem that you can practice with using loops:

Problem:
Write a program that asks the user for a number, n, and prints the sum of all numbers from 1 to n.

Example output:
Enter a number: 5
The sum of numbers from 1 to 5 is 15.

To solve this problem, you can use a loop to iterate from 1 to the given number, and keep adding the numbers to a running sum.

Here's a Python code solution:

Python:
n = int(input("Enter a number: "))

# initialize sum variable
sum = 0

# iterate from 1 to n
for i in range(1, n+1):
    sum += i

# print the result
print("The sum of numbers from 1 to", n, "is", sum)

In this code, we use a for loop to iterate from 1 to n (inclusive). For each iteration, we add the current number to the sum variable. Finally, we print the result.

You can try running this code and test it with different input values. It should give you the correct sum of numbers from 1 to the given number. This problem is a good exercise to practice working with loops and accumulating values in a variable.
 

About this Thread

  • 3
    Replies
  • 439
    Views
  • 2
    Participants
Last reply from:
BrigthWin

Trending Topics

Online now

Members online
496
Guests online
2,592
Total visitors
3,088

Forum statistics

Threads
2,303,977
Posts
29,157,192
Members
1,197,613
Latest member
farahfawwcet
Back
Top