EXERCISE - 3 CONTROL FLOW
A) Write a Program for checking whether the given number is a even number or not.
B) Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10
C) Write a program using a for loop that loops over a sequence. What is sequence ?
D) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero.
A) AIM: Write a program for checking whether the given number is an even number or not.
SOURCE CODE:
n=int(input("Enter number:"))
if (n%2==0):
print(n," is an even number.")
else:
print(n," is not an even number.")
OUTPUT:
Enter number:5
5 is not an even number.
B) AIM: Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10.
SOURCE CODE:
print("Decimal Equivalents of:")
for i in range(2,11):
dec_eq=1/i
print("1/"+str(i)+"= ",dec_eq)
OUTPUT:
Decimal Equivalents of:
1/2= 0.5
1/3= 0.3333333333333333
1/4= 0.25
1/5= 0.2
1/6= 0.16666666666666666
1/7= 0.14285714285714285
1/8= 0.125
1/9= 0.1111111111111111
1/10= 0.1
C) AIM: Write a program using a for loop that loops over a sequence. What is sequence?
SEQUENCE IN PYTHON:
In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python; the following three are the most important.
A) Write a Program for checking whether the given number is a even number or not.
B) Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10
C) Write a program using a for loop that loops over a sequence. What is sequence ?
D) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero.
A) AIM: Write a program for checking whether the given number is an even number or not.
SOURCE CODE:
n=int(input("Enter number:"))
if (n%2==0):
print(n," is an even number.")
else:
print(n," is not an even number.")
OUTPUT:
Enter number:5
5 is not an even number.
B) AIM: Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10.
SOURCE CODE:
print("Decimal Equivalents of:")
for i in range(2,11):
dec_eq=1/i
print("1/"+str(i)+"= ",dec_eq)
OUTPUT:
Decimal Equivalents of:
1/2= 0.5
1/3= 0.3333333333333333
1/4= 0.25
1/5= 0.2
1/6= 0.16666666666666666
1/7= 0.14285714285714285
1/8= 0.125
1/9= 0.1111111111111111
1/10= 0.1
C) AIM: Write a program using a for loop that loops over a sequence. What is sequence?
SEQUENCE IN PYTHON:
In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python; the following three are the most important.
- Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted.
- Tuples are like lists, but they are immutable - they can't be changed.
- Strings are a special type of sequence that can only store characters, and they have a special notation. However, all of the sequence operations described below can also be used on strings.
days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
print("Days of the week:")
for day in days:
print(day)
OUTPUT:
Days of the week:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
D) AIM: Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero.
SOURCE CODE:
num=int(input("Enter number:"))
print("Countdown Starts:")
while(num>=0):
print(num)
num=num-1
OUTPUT:
Enter number:5
Countdown Starts:
5
4
3
2
1
0