Quizzma Latest Questions

What is the output of the following program?

What is the output of the following program?

def sum_to(num):
sum = 0
for i in range(num+1):
sum += i
print(sum)

x = 5
sum_to(x)
print(x)

5
15
15
5
5
15




Leave an answer

Leave an answer

1 Answer

  1. The output of the program will be:

    
    

    15

    5

    Explanation:
    1. The function `sum_to(num)` calculates the sum of all integers from 0 to `num`. When you call `sum_to(5)`, it sums 0 + 1 + 2 + 3 + 4 + 5, which equals 15. It then prints this sum.
    2. After the function call, `print(x)` outputs the value of `x`, which is still 5.

    So, the printed lines are 15 (from the function) followed by 5 (from the print statement).

Related Questions