Q1: What is the difference between else and elif construct of if statement?
Ans: if..else statement allows us to write two alternative paths and the control condition determines which path gets executed. If the condition is incorrect/False then else statement will execute. The syntax for if..else statement is as follows.
if condition:
statement(s)
else:
statement(s)
Many a times there are situations that require multiple conditions to be checked and it may lead to many alternatives. In such cases we can make a chain of conditions using elif. Syntax of elif is shown below:
if condition :
statement
elif condition :
statement
elif condition :
statement
|
|
|
|
else :
statement
Q2: What is the purpose of range() function? Give one example.
Ans: range( ) function simply generates/return a sequence of number from starting number(by default 0) to one less than end number. This function is commonly used in for loop. for example
for i in range(3, 7):
print(i)
OUTPUT :
3
4
5
6
Q3: Differentiate between break and continue statements using examples.
Ans: break statement is used for immediate termination of loop for example
for i in range(7):
if i==3:
break
print(i)
print("Loop Terminate")
OUTPUT :
0
1
2
Loop Terminate
continue statement is used to skip the current iteration of the loop and pass the control to the next iteration. for example :
for i in range(7):
if i==3:
continue
print(i)
print("Loop Terminate")
OUTPUT :
0
1
2
4
5
6
Loop Terminate
Q4: What is an infinite loop? Give one example.
Ans: A never ending loop is called infinite loop. for example the following loop will print ‘A’ infinite times
i=0
while(i<=0):
print("A")
Q5: Find the output of the following program segments:
(i) a = 110
while a > 100:
print(a)
a -= 2
Ans:
110
108
106
104
102(ii) for i in range(20, 30, 2):
print(i)
Ans:
20
22
24
26
28(iii) country = 'INDIA'
for i in country:
print (i)
Ans:
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)
Ans: 12
(v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Ans:
2
3
4
4
6
8
6
9(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
Ans:
OUTPUT:
Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4
Q1: 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).
Ans:
nm = input("Enter your name")
age = int(input("Enter your age"))
if age >= 18:
print("You are eligible for driving licence")
else:
print("You are not eligible for driving licence")
Q2: Write a function to print the table of a given number. The number has to be entered by the user.
Ans:
def table(n):
for i in range(1, 11):
print("7 * ", i," =", n*i)
num = int(input("Enter any number"))
table(num)
OUTPUT : (If number entered by user is 7)
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Q3: Write a program that prints minimum and maximum of five numbers entered by the user.
Ans:
num1 = int(input("Enter any number"))
max=num1
min=num1
for i in range(4):
num = int(input("Enter any number"))
if max < num: max = num if min > num:
min=num
print("Minimum number is :",min)
print("Maximum number is :",max)
Q4: Write a program to check if the year entered by the user is a leap year or not.
Ans:
yr = int(input("Enter any year"))
if yr0 == 0:
if yr@0 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
elif yr%4 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
Q5: Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer input by the user.
Ans:
num = int(input("Enter any number"))
for i in range(1,num + 1):
if i%2 == 0:
print(5 *i, end = " , ")
else:
print(5*i*(-1), end = " , ")
Q6: Write a program to find the sum of 1+ 1/8 +1/27……1/n3, where n is the number input by the user.
Ans:
num = int(input("Enter any number"))
sum = 0
for i in range(1,num + 1):
sum = sum + 1/i**3
print("Sum of series is : ", sum)
Q7: Write a program to find the sum of digits of an integer number, input by the user.
Ans:
num = int(input("Enter any number"))
sum = 0
while(num):
r = num % 10
sum = sum + r
num = num // 10
print("Sum of the digit is : ",sum)
Q8: Write a function that checks whether an input number is a palindrome or not.
Ans:
num = input("Enter any number")
rnum=num[: : -1]
if num == rnum:
print("Palindrome")
else:
print("Not a Palindrome")
OR
num = int(input("Enter any number"))
rnum = 0
r = 0
onum = num
while(num):
r = num % 10
rnum = rnum * 10 + r
num = num // 10
if onum == rnum :
print("Palindrome")
else :
print("Not Palindrome")
Q9: Write a program to print the following patterns:
Ans:
i)
n = 3for i in range (1, n + 1):
sp = (n - i) * " "
sym = (2 * i - 1) * "*"
print(sp, sym)
for j in range(n - 1, 0, -1):
sp = (n - j) * " "
sym = (2 * j - 1)* "*"
print(sp, sym)
ii)
num = 5
for i in range (1, num + 1):
sp = (num - i) * " "
print(sp, end = " ")
for k in range(i, 1, -1):
print(k, end = " ")
for j in range(1, i + 1):
print(j, end = " ")
print()
iii)
str = "123456"
for i in (str):
print(" " * int(i) + str[0 : len(str) - int(i)])
OR
num = 5
for i in range (num, 0, -1):
sp = (num - i) * " "
print(sp, end=" ")
for k in range(1, i + 1):
print(k, end=" ")
print()
iv)
num = 3
k = 0
for i in range (1, num + 1):
sp = (num - i)*" "
print(sp, end=' ')
while (k != (2 * i - 1)) :
if (k == 0 or k == 2 * i - 2) :
print("*", end= "")
else :
print(" ", end = "")
k = k + 1
k = 0
print( )
for j in range (num - 1, 0, -1):
sp = (num - j) * " "
print(sp, end=" ")
k = (2 * j - 1)
while (k > 0) :
if (k==1 or k == 2 * j - 1):
print("*",end="")
else:
print(" ",end="")
k = k - 1
print( )
Q10: 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 the marks obtained by the student is input to the program.
Ans:
per = int(input("Enter the Percentage"))
if per > 90:
gr="A"
elif per > 80 and per <= 90:
gr = "B"
elif per > 70 and per <= 80:
gr = "C"
elif per >= 60 and per <= 70:
gr = "D"
elif per < 60:
gr = "E"
print("Grade is : ", gr)
Q1: Write a menu driven program that has options to
Ans:
ch='y'
while (ch=='y'):
print("Menu Driven Program")
print("1. Enter Marks of Five Subjects")
print("2. Calculate Percentage")
print("3. Calculate Grade")
print("4. Exit")
num = int(input("Enter Your Choice"))
if num == 1 :
en = int(input("Enter marks of English"))
m = int(input("Enter marks of Mathematics"))
sc = int(input("Enter marks of Science"))
h = int(input("Enter marks of Hindi"))
sst = int(input("Enter marks of SST"))
print("Marks in English is :",en)
print("Marks in Hindi is :",h)
print("Marks in Mathematics is :",m)
print("Marks in Science is :",sc)
print("Marks in Social Studies is :",sst)
if num == 2 :
sum = en + m + sc + h + sst
per = sum/5
print("Percentage is ,", per)
if num == 3 :
if per >= 85 :
print("Grade is A")
elif per >= 75 and per < 85:
print("Grade is B")
elif per >= 50 and per < 75 :
print("Grade is C")
elif per > 30 and per < 50:
print("Grade is D")
elif per < 30:
print("Reappear")
if num == 4:
exit()
33 docs|11 tests
|
1. What is the significance of the flow of control in programming? | ![]() |
2. What are the different types of control structures used in programming? | ![]() |
3. How do conditional statements work in programming? | ![]() |
4. Can you explain the role of loops in controlling the flow of a program? | ![]() |
5. What are the common errors associated with flow control in programming? | ![]() |