NCRT Computer
NCRT Computer
Question 1:
ANSWER:
‘Else’ is used along with ‘if’ to define the alternative
path if the condition mentioned in the ‘if’ statement is
incorrect. This means that only one statement can be
evaluated using if..else statement.
Page No 139:
Question 2:
ANSWER:
The range() function is a built-in function of Python. It
is used to create a list containing a sequence of integers
from the given start value to stop value (excluding stop
value). This is often used in for loop for generating the
sequence of numbers.
​Example:
for num in range(0,5):
print (num)
OUTPUT:
0
1
2
3
​4
Page No 139:
ANSWER:
The 'break' statement alters the normal flow of
execution as it terminates the current loop and
resumes execution of the statement following that
loop.
Example:
num = 0
for num in range(5):
num = num + 1
if num == 3:
break
print('Num has value ' , num)
print('Encountered break!! Out of loop')
OUTPUT:
OUTPUT:
Page No 139:
Question 4:
ANSWER:
The statement within the body of a loop must ensure
that the test condition for the loop eventually becomes
false, otherwise, the loop will run infinitely. Hence, the
loop which doesn’t end is called an infinite loop. This
leads to a logical error in the program.
Example:
num = 20
while num > 10:
print(num)
num = num + 1
Page No 139:
Question 5:
ANSWER:
i) The value of ‘a’ is decreased by 2 on each iteration of
the loop and when it reaches 100, the condition a >100
becomes false and the loop terminates.
OUTPUT:
110
108
106
104
102
iv) The ‘while’ loop will run till the value of 'i' is 8, and
the condition in ‘if’ function will return true for two
values i.e. 4 and 8, which will be added to ‘sum’. The
last statement will print the value of the variable ‘sum’.
OUTPUT:
12
OUTPUT:
2
3
4
4
6
8
6
9
vi) OUTPUT:
Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4
Page No 140:
Question 1:
ANSWER:
Program:
#Program to check the eligibility for driving license
name = input("What is your name? ")
age = int(input("What is your age? "))
OUTPUT:
What is your name? John
What is your age? 23
You are eligible to apply for the driving license.
Page No 140:
Question 2:
ANSWER:
Program:
#Program to print the table of a number
num = int(input("Enter the number to display its table:
"))
print("Table: ");
for a in range(1,11):
print(num," × ",a," = ",(num*a))
OUTPUT:
Enter the number to display its table: 5
Table:
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50
Page No 140:
Question 3:
ANSWER:
Program:
#Program to input five numbers and print largest and
smallest numbers
smallest = 0
largest = 0
for a in range(0,5):
x = int(input("Enter the number: "))
if a == 0:
smallest = largest = x
if(x < smallest):
smallest = x
if(x > largest):
largest = x
print("The smallest number is",smallest)
print("The largest number is ",largest)):
Explanation of Program:
The first ‘if’ loop will assign the first value entered by
the user to the smallest and largest variable.
Page No 140:
Question 4:
ANSWER:
The year is a leap year if it is divisible by 4.
Program:
year = int(input("Enter the year: "))
if(year % 4 == 0):
print("The year",year," is a leap year.")
else:
print("The year",year," is not a leap year.")
OUTPUT:
Enter the year: 2020
The year 2020 is a leap year.
Page No 140:
Question 5:
ANSWER:
The given sequence is: –5, 10, –15, 20, –20
The numbers in the sequence are multiples of 5 and
each number at the odd place is negative, while the
number at even place is positive. The series can be
generated by checking each index for odd-even and if it
is odd, it will be multiplied by '-1'.
Program:
num = int(input("Enter the number: "))
for a in range(1,num+1):
if(a%2 == 0):
print(a * 5, end=",")
else:
print(a * 5 * (-1),end=",")
Program:
num = int(input("Enter the number: "))
for a in range(1,num+1):
print(a*5*(-1)**a, end=",")
OUTPUT:
Enter the number: 6
-5,10,-15,20,-25,30
Page No 140:
Question 6:
ANSWER:
Program:
sum = 0
#Getting the number input from user
n = int(input("Enter the number: "))
for a in range(1,n+1):
#calculating the sum
sum = sum + (1/pow(a,3))
#Sum of the series is
print("The sum of the series is: ",round(sum,2))
OUTPUT:
Enter the number: 5
The sum of the series is: 1.19
Page No 141:
Question 7:
ANSWER:
The program can be written in two ways.
1. The number entered by the user can be converted to
an integer and then by using 'modulus' and 'floor'
operator it can be added digit by digit to a variable
'sum'.
2. The number is iterated as a string and just before
adding it to 'sum' variable, the character is converted
to the integer data type.
Program 1:
#Program to find sum of digits of an integer number
#Initializing the sum to zero
sum = 0
#Getting user input
n = int(input("Enter the number: "))
# looping through each digit of the number
# Modulo by 10 will give the first digit and
# floor operator decreases the digit 1 by 1
while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
# Printing the sum
print("The sum of digits of the number is",sum)
OUTPUT:
Enter the number: 23
The sum of digits of the number is 5
Program 2:
​​#Initializing the sum to zero
sum = 0
#Asking the user for input and storing it as a string
n = input("Enter the number: ")
#looping through each digit of the string
#Converting it to int and then adding it to sum
for i in n:
sum = sum + int(i)
Page No 141:
Question 8:
ANSWER:
Program:
#program to find if the number is a palindrome
rev = 0
n = int(input("Enter the number: "))
#Storing the number input by user in a temp variable to
run the loop
temp = n
#Using while function to reverse the number
while temp > 0:
digit = (temp % 10)
rev = (rev * 10) + digit
temp = temp // 10
#Checking if original number and reversed number are
equal
if(n == rev):
print("The entered number is a palindrome.")
else:
print("The entered number is not a palindrome.")
OUTPUT:
Enter the Number: 114411
The entered number is a palindrome.
Page No 141:
Question 9:
ANSWER:
i)
ii)
n=5
for i in range (1, n + 1):
#This will print the space before the number
space = (n - i) * " "
print(space, end = " ")
#This for loop will print the decreasing numbers
for k in range(i, 1, -1):
print(k, end = " ")
#This for loop will print the increasing numbers
for j in range(1, i + 1):
print(j, end = " ")
#Print a new line after the space and numbers are
printed
print()
iii)
n=5
for i in range (n, 0, -1):
# The space will increase on each iteration as i
value will decrease
space = (n - i)*" "
print(space, end=" ")
#This for loop will print the increasing numbers
for j in range(1, i + 1):
print(j, end=" ")
print()
iv)
n=3
k=0
for i in range (1, n + 1):
space = (n - i)*" "
print(space, end=' ')
while (k != (2 * i - 1)) :
#This will print the star at the start and end of
every line
if (k == 0 or k == 2 * i - 2) :
print('*', end= "")
#This will print the space in the middle
else :
print(' ', end = "")
k=k+1
k=0
print()
#This will print the bottom half of the hollow diamond
shape
for j in range (n - 1, 0, -1):
space = (n - j)*" "
print(space, end=" ")
k = (2 * j - 1)
while (k > 0) :
#This will print the star at the start and end of
every line
if (k==1 or k == 2*j-1):
print("*",end="")
#This will print the space in the middle
else:
print(" ",end="")
k=k-1
print()
Page No 141:
Question 10:
Percentage of Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E
ANSWER:
Program:
#Program to print the grade of the student
n = float(input('Enter the percentage of the student: '))