0% found this document useful (0 votes)
23 views

CSCI101 Lab02 SelectionAndLooping

Uploaded by

s-zeyad.mawjoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CSCI101 Lab02 SelectionAndLooping

Uploaded by

s-zeyad.mawjoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

University of Science and Technology

Communications & Information Engineering Program


CSCI 101: Introduction to Computer Science
Selection and Looping

1. Take a number from the user and print if it is odd number or even number.

2. What is the problem with the following code:


r = float(input('enter radius:'))
if r<=0:
print(invalid)
else:
area = pi * r * r
print('area')

3. What is the output of the following code


for i in range(1,4):
print(i)
i = 10
print(i)

4. What is the values of i and x after executing the following code


N = 6; x=0
for i in range(1,N):
x = x + i

5. What is the output of this code in the cases shown below


for i in range(start, end, step):
print(i)
a) start = 1, end = 9, step = 2
b) start = 9, end = 1, step = 2
c) start = 9, end = 1, step = -2
d) start = 1, end = 9, step = 0

6. Write a program that reads a number N from the user and prints the sum of the
series: Sum = 1^2 + 2^2 + 3^2 + 4^2 + . . . N^2

7. Write a program to get a number N from the user. If it is negative a message should
be displayed that the number is negative. Otherwise: it should print the summation
of the numbers from 1 to N.

Page 1
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Selection and Looping

8. Write a program that reads two integer values N1 and N2 and calculates and prints
the summation of numbers between N1 and N2 and their average.
Example1: user enters N1=3 , N2=7 ➔ sum = 25 and average = 5
Example2: user enters N1=10 , N2=7 ➔ sum = 34 and average = 8.5

9. Write a program that reads N numbers from the user and prints the average of the
elements greater than 6.

10. Write a program to read N numbers from the user and print the sum of the odd
elements and the sum of the even elements.

11. Write a program that gets two positive integer numbers X and Y and calculates
X^Y. (Hint: using loops with multiplication, i.e., X^Y = X*X*X*… Y times)

12. Write a program that reads the departure time (hours and minutes) of a train and the
arrival time (hours and minutes) and computes and displays the trip time.
(Example: for Departure time 10:50 and Arrival time 12:10 the trip time will be
1 hr and 20 min)

13. Guess Game: Write a program that generates a random number from 10 to 20. It
then gives the user 5 chances to guess the generated number and prints either “Try
again” for wrong guess or “Well Done” for correct guess.
Hint: To generate a random number from 10 to 20 write:
import random
num = random.randint(10,20)

Search: How can you stop the loop once the user enters a correct guess?

Page 2

You might also like