CBSE · 083Class XII · 2025Section B2 marks

Question 26

4 March 2025 · Computer Science (083)

The code given below accepts N as an integer argument and returns the sum of all integers from 1 to N. Observe the following code carefully and rewrite it after removing all syntax and logical errors.

python
def Sum(N)
    for I in range(N):
    S=S+I
    return S
print(Sum(10))

Answer

python
def Sum(N):
    S=0
    for I in range(1, N+1):
        S=S+I
    return S
print(Sum(10))

Explanation

Added colon to def, initialized S=0, corrected range to include N (1, N+1), and indented print call.