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

Notes Flow of Control

The document contains code snippets demonstrating the use of conditional statements like if-else and for loops to: 1) Check if a user's age is greater than 18 and print if they are eligible to vote or not. 2) Calculate the positive difference between two numbers. 3) Check if a number is positive, negative or zero. 4) Create a basic four function calculator. 5) Print even numbers from a given list using a for loop. 6) Print each character in the word 'PYTHON' using a for loop.

Uploaded by

motorcop6
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Notes Flow of Control

The document contains code snippets demonstrating the use of conditional statements like if-else and for loops to: 1) Check if a user's age is greater than 18 and print if they are eligible to vote or not. 2) Calculate the positive difference between two numbers. 3) Check if a number is positive, negative or zero. 4) Create a basic four function calculator. 5) Print even numbers from a given list using a for loop. 6) Print each character in the word 'PYTHON' using a for loop.

Uploaded by

motorcop6
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Let us now modify the example on voting with the

condition that if the age entered by the user is greater


than 18, then to display that the user is eligible to vote.
Otherwise display that the user is not eligible to vote.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")

Program 6-2 Program to print the positive difference

of two numbers.

#Program 6-2
#Program to print the positive difference of two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
diff = num1 - num2
else:
diff = num2 - num1
print("The difference of",num1,"and",num2,"is",diff)
Output:
Enter first number: 5
Enter second number: 6
The difference of 5 and 6 is 1

num=int(input("enter a interger"))
if num>0:
print("number is positive")
elif num==0:
print("number is equal zero")
else :
print("number is less than zero")
#Program to create a four function calculator
result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program
terminated")
else:
result = val1/val2
else:
print("Wrong input,program terminated")
print("The result is ",result)

Program 6-8 Program to print even numbers in a given

sequence using for loop.

#Program 6-8
#Print even numbers in the given sequence
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')
Output:
2 is an even Number
4 is an even Number
#Program 6-6
#Print the characters in word PYTHON using for loop
for letter in 'PYTHON':
print(letter)
Output:
P
Y
T
H
O
N

You might also like