Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Rao has written a code to input a number and check whether it is prime or not. His code is having errors. Rewrite the correct code and underline the corrections made.

def prime():

n=int(input("Enter number to check :: ")

for i in range (2, n//2):

if n%i=0:

print("Number is not prime \n")

break

else:

print("Number is prime \n’)

 

Answer:

Corrected Code with underlined corrections:

def prime ():

    n=int(input( "Enter number to check :: " ) )

    for i in range ( 2 , n// 2 ):

        if n%i = = 0 :

            print( "Number is not prime \n" )

            break

    else:

        print( "Number is prime \n " )

The corrections are:

  • In the line,  n=int(input( "Enter number to check :: " ) ) , the underlined bracket was missing.
  • In the line, if n%i = = 0 : , the underlined equal to symbol was missing.
  • In the line, break , the indentation of break was incorrect.
  • In the line, print( "Number is prime \n " ) , the quotes inside the print( ) did not match.
Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.