0% found this document useful (0 votes)
14 views25 pages

NCRT Computer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views25 pages

NCRT Computer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Page No 139:

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.

If more than one statements need to be evaluated,


‘elif’ construct is used. There is no limit on the number
of ‘elif’ construct which should be used in a statement.
However, the conditions mentioned in each ‘elif’
construct should be mutually exclusive i.e. if one of the
conditions in if..elif is correct it should imply that all the
other conditions are false.

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:

Num has value 1


Num has value 2
Encountered break!! Out of loop

When a 'continue' statement is encountered, the


control skips the execution of remaining statements
inside the body of the loop for the current iteration and
jumps to the beginning of the loop for the next
iteration. If the loop’s condition is still true, the loop is
entered again, else the control is transferred to the
statement immediately following the loop.

#Prints values from 0 to 5 except 3


num = 0
for num in range(5):
num = num + 1
if num == 3:
continue
print('Num has value ' ,num)
print('End of loop')

OUTPUT:

Num has value 1


Num has value 2
Num has value 4
End of loop

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

​The above statement will create an infinite loop as


the value of ‘num’ initially is 20 and each subsequent
loop is increasing the value of num by 1, thus making
the test condition num > 10 always true.

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

ii) The range(start, stop, step) function will create a list


from the start value to stop value(excluding the stop
value) with a difference of the step value.
The list thus created will be [20, 22, 24, 26, 28].
OUTPUT:
20
22
24
26
28
iii) The variable 'country' will act as a list of characters
for the loop to iterate.
OUTPUT:
I
N
D
I
A

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

v) The first loop will iterate over the list [1, 2, 3]


The second for loop will iterate over the list [2, 3, 4]
The print statement is printing the value of x * y i.e.
[1*2, 1*3, 1*4, 2*2, 2*3, 2*4, 3*2, 3*3, 3*4] which
comes out to be [2, 3, 4, 4, 6, 8, 6, 9, 12].
The break statement will terminate the loop once the
value of x * y is greater than 10, hence, the output
value will be till 9.

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? "))

#Checking the age and displaying the message


accordingly
if age >= 18:
print("You are eligible to apply for the driving
license.")
else:
print("You are not eligible to apply for the driving
license.")

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.

The subsequent if loop will check the next number


entered and if it is smaller than the previous value, it
will be assigned to ‘smallest’ variable, and if greater
than the previous value it will be assigned to 'largest'
variable.

After the end of the loop, the values of the ‘smallest’


and ‘largest’ variable are printed.
OUTPUT:
Enter the number: 10
Enter the number: 5
Enter the number: 22
Enter the number: 50
Enter the number: 7
The smallest number is 5
The largest number is 50

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=",")

The print(a*5*(-1)**a) statement can be also used


inside 'for' loop as for even values of a, (-1)a will return
'1' and for odd, it will return (-1).

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)

# Printing the sum


print("The sum of digits of the number is",sum)
OUTPUT:
Enter the number: 44
The sum of digits of the number is 8

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)

# n = 3 Number of lines from top to middle of diamond


shape
n=3
for i in range (1, n + 1):
# This will print the space
space = (n - i)*" "
# It will print the star
star = (2 * i - 1)""
print(space,star)
# The bottom half will be drawn using this loop
for j in range(n - 1, 0, -1):
space = (n - j)*" "
star = (2 * j - 1)""
print(space, star)

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: '))

if(n > 90):


print("Grade A")
elif(n > 80):
print("Grade B")
elif(n > 70):
print("Grade C")
elif(n >= 60):
print("Grade D")
else:
print("Grade E")
OUTPUT:
Enter the percentage of the number: 60
Grade D

You might also like