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

Example Program For Lab Excercises

The document provides example programs for basic conditional statements in programming, including 'if', 'if-else', 'if-elif-else', and nested conditions. Each section includes an algorithm and corresponding code that demonstrates how to check for positive numbers, even or odd numbers, and assign grades based on marks. The examples are designed for lab exercises to help users understand conditional logic in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Example Program For Lab Excercises

The document provides example programs for basic conditional statements in programming, including 'if', 'if-else', 'if-elif-else', and nested conditions. Each section includes an algorithm and corresponding code that demonstrates how to check for positive numbers, even or odd numbers, and assign grades based on marks. The examples are designed for lab exercises to help users understand conditional logic in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

EXAMPLE PROGRAM FOR LAB EXCERCISES

if CONDITION PROGRAM
Algorithm
1. Start
2. Read a number from the user
3. Check if the number is greater than 0
4. If true, print “The number is positive”
5. Stop
Program:
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive")

if else
Algorithm
1. Start
2. Read a number from the user
3. Check if the number is even
4. If true, print “The number is Even”
5. Else, print “The number is Odd”
6. Stop
Program:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is Even")
else:
print("The number is Odd")
if elif else
Algorithm
1. Start
2. Read marks from the user
3. If marks ≥ 90, print “Grade A”
4. Else if marks ≥ 75, print “Grade B”
5. Else if marks ≥ 50, print “Grade C”
6. Else, print “Fail”
7. Stop
Program:
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")

Nested Condition:
Algorithm
1. Start
2. Read a number from the user
3. Check if the number is greater than or equal to 0
4. If true, check whether the number is even
5. If even, print “Positive Even Number”
6. Else, print “Positive Odd Number”
7. Else, print “Negative Number”
8. Stop
Program:
num = int(input("Enter a number: "))
if num >= 0:
if num % 2 == 0:
print("Positive Even Number")
else:
print("Positive Odd Number")
else:
print("Negative Number")

You might also like