CSCI101 Lab02 SelectionAndLooping
CSCI101 Lab02 SelectionAndLooping
1. Take a number from the user and print if it is odd number or even number.
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