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

Status
Not open for further replies.

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.
 
Status
Not open for further replies.

About this Thread

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

Online now

Members online
336
Guests online
4,892
Total visitors
5,228

Forum statistics

Threads
2,350,208
Posts
29,366,163
Members
1,089,584
Latest member
N0o0b0o0d0Y
Back
Top