Test Index
CBSE Class 12 Computer Science 2025 Paper Compartment
© examsnet.com
Question : 26 of 37
Marks:
+1,
-0
The code provided below is intended to input a positive integer from the user and display the total number of its factors. However, there are syntax and logical errors in the code. Rewrite the code after removing all the errors. Underline all the corrections made.
n=int(input("Enter a positive integer:")c=0for i in range(n+1): if n%i=0: c+=1print(c)
Solution:
Corrected code (corrections underlined):
n=int(input("Enter a positive integer:"))c=0for i in range(1,n+1): if n%i==0: c+=1print(c)Errors corrected:1. int(input("Enter a positive integer:"): a closing bracket is missing (syntax error) → add ).2. range(n+1) starts at 0, and n%0 gives ZeroDivisionError (logical error) → range(1,n+1) checks 1 to n.3. if n%i=0: uses assignment = in a condition (syntax error) → ==.4. c+=1 must be indented inside the if block (IndentationError).Check: for input 12 the factors are 1, 2, 3, 4, 6, 12, so the program prints 6.© examsnet.com
Go to Question: