[go: up one dir, main page]

0% found this document useful (0 votes)
21 views3 pages

Revision Midterm Questions

Uploaded by

shibnariyas
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)
21 views3 pages

Revision Midterm Questions

Uploaded by

shibnariyas
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/ 3

Revision

Write the following programs

1) sum of even numbers greater than 15. User enters ‘q’ to quit.
2) sum=0
3) while True:
4) n=input("Enter a number (or q to quit):")
5) if n=="q":
6) break
7) n=int(n)
8) if n>15 and n%2==0:
9) sum+=n
10) print("Sum of even numbers greater than 15 is {}".format(sum))

11) count of even numbers. User enters ‘q’ to quit.
12) count=0
13) while True:
14) n=input("Enter a number (or q to quit):")
15) if n=="q":
16) break
17) n=int(n)
18) if n%2==0:
19) count+=1
20) print("Count of even numbers is {}".format(count))
3)start end numbers are specified to get an equation as given below
Start : 2
End: 5

2*2= 4
3*3= 9
4*4=16
5*5=25

- def num(start,end):
- for i in range(start,end+1):
- print("{0} * {1} = {2}".format(i,i,i*i))
- start=2
- end=5
- num(start,end)
-

4)loop ends when user enters the letter 'q' ( use while :True )
while True:
n=input("Enter integer (or q to break):")
if n=="q":
break

5) Loop stops only when user enters a positive number or zero ( use while :True )

while True:
n=int(input("Enter integer (or q to break):"))
if n==0 or n%2==0:
break

6) Write a program with a function that generates a pattern given below


*

* *

* * *

* * * *

* * * * *

- for i in range(0,5):
for j in range(0,i+1):
print("*", end=" ")
print("\n")

7)Write a function that will interact with the user to accept n numbers and returns the largest and
the smallest value ( do not use inbuild functions min and max)

 n=int(input("Enter the first number of elements: "))


 largest=int(input("Enter the first number: "))
 smallest=largest
 for i in range(0,n-1):
 num=int(input("Enter next number: "))
 if num>largest:
 largest=num
 if num<smallest:
 smallest=num
 print("The largest number is: {0}".format(largest))
 print("The smallest number is: {0}".format(smallest))

8)Write a program that will accept 5 marks for the subject and to display the grade for each ina
table format using the grade function covered in class ( use loop and function)
Enter mark 1: 12
Enter mark 2: 56
Enter mark 3: 67
Enter mark 4: 89
Enter mark 5: 89

Mark: Grade:

12 D
56 C
67 B
89 A
89 A

9)Trace the below program .consider variable i,j, start number,and output as given in the excel
sheet.

for i in range(1, 6):


# display the ith line
# the first number on line i is 2i
start_number = 2 * i
# print from start number down to 1
for j in range(0, start_number):
number = start_number - j
print(number, end=" ")
# print a new line to complete the line i print()

10)Predict the output of the following by tracing and then check


first=3
iter=3
for i in range(0,iter+1):
first=2*first*i + 3*i
print (first)

You might also like