हिंदी

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 - Computer Science (Python)

Advertisements
Advertisements

प्रश्न

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. Comment on all the corrections made.

def Sum(N)
    for I in range(N):
        S=S+I
    return S
print(Sum(10)
कोड लेखन
Advertisements

उत्तर

def Sum(N): # Correction: Added colon (:)
         S = 0 # Correction: Initialized S to 0
         for I in range(1,(N+1)): # Correction: Fixed range and added colon (:)
              S=S+I
         return S # Correction: Proper indentation
print(Sum(10)) # Correction: Added closing bracket
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2024-2025 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×