NCERT Solutions Class 11 Computer Science Chapter 6 – Flow of Control
Question1. What is the difference between else and elif construct of if statement?
Answer:
Else | elif |
Else is a part of if statement. | The elif is short for else if. |
Else statement is executed whenever the if statement is getting false. | Elif executed block of code whenever one of the condition evaluate true. |
Example:If(a<b)Print “a less than b”ElsePrint “ a is not less than b” | Example:If num> 0;Print “positive number”Elifnum == 0;Print “zero”Else:Print “negative number” |
Question 2. What is the purpose of range() function? Give one example.
Answer:
Function range() is used for creating a list containing the integer values in sequence from start value to stop value. Sometimes it is used for generating the number sequentially in for loop.
Example:
Question 3. Differentiate between break and continue statements using examples.
Answer:
Break Statement | Continue Statement |
Break statement is to break the loop. | Continue statement is used to to continue the loop |
Break statement is used with switch statement | Continue statement is not used with switch statements |
Keyword: break | Keyword: continue |
Question 4. What is an infinite loop? Give one example.
Answer:
- Infinite loop execute when while condition never become false
- If condition remain true. the loop will never get terminated.
- The control enters in loop and keeps repeating the same block code and loop never end.
Example:
a = 1
while a==1:
b = input(“what’s your birthdate?”)
print(“your birthdate is ”, b)
Output:
What’s your birthdate?
13
your birthdate is 13
What’s your birthdate?
14
your birthdate is 14
What’s your birthdate?
15
your birthdate is 15
What’s your birthdate?
16
your birthdate is 16
:
:
:
Question 5. Find the output of the following program segments:
(i) a = 110
while a > 100:
print(a)
a -= 2
Output: 110
108
106
104
(ii) for i in range(20,30,2):
print(i)
Output: 20
22
24
26
28
(iii) country = ‘INDIA’
for i in country:
print (i)
Output:
I
N
D
I
A
(iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
Output: 12
- v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Output:
2
3
4
4
6
8
6
9
(v) var = 7
whilevar> 0:
print (‘Current variable value: ‘, var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print (“Good bye!”)
Output:
Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4
Programming Exercises
Question 1. Write a program that takes the name and age of the user as input and displays a message whether the user is eligible to apply for a driving license or not. (the eligible age is 18 years).
Answer:
Question 2. Write a function to print the table of a given number. The number has to be entered by the user.
Answer:
Question 3. Write a program that prints minimum and maximum of five numbers entered by the user.
Answer:
Question 4. Write a program to check if the year entered by the user is a leap year or not.
Question 5. Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer input by the user.
Question 6. Write a program to find the sum of 1+ 1/8 + 1/27.
Answer:
Question 7.Write a program to find the sum of digits of an integer number, input by the user.
Answer:
Question 8. Write a function that checks whether an input number is a palindrome or not. [Note: A number or a string is called palindrome if it appears same when written in reverse order also. For example, 12321 is a palindrome while 123421 is not a palindrome]
Program:
rev = 0
n = int(input(“Enter the number: “))
temp = n
while temp > 0:
digit = (temp % 10)
rev = (rev * 10) + digit
temp = temp // 10
if(n == rev):
print(“The number is a palindrome.”)
else:
print(“The number is not a palindrome.”)
output:
Enter the number: 5665
The number is a palindrome.
Question 9. Write a program to print the following patterns
Answer:
(i)
(ii)
(iii)
(iv)
Question 10:Write a program to find the grade of a student when grades are allocated as given in the table below. Percentage of the marks obtained by the student is input to the program.
Percentage of Marks | Grade |
Above 90% | A |
80% to 90% | B |
70% to 80% | C |
60% to 70% | D |
Below 60% | E |
Answer:
Program:
OUTPUT:
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: 91
Grade A
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.