🐍 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
  • 427
    Views
  • 2
    Participants
Last reply from:
BrigthWin

Trending Topics

Online now

Members online
948
Guests online
1,071
Total visitors
2,019

Forum statistics

Threads
2,273,869
Posts
28,952,155
Members
1,234,981
Latest member
AprilSiading
Back
Top