What's new

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

BrigthWin

Forum Veteran
Elite
Joined
Jul 21, 2020
Posts
1,638
Reaction
1,768
Points
650
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.
 
Back
Top