python lab
python lab
Algorithm: Flowchart:
Step1: Start
Step 2: Input: Read the number of terms n from the user.
Step 3: Initialize:
Set a to 0 (first term).
Set b to 1 (second term).
Set counter i to 1.
Step 4: Process:
Step 5: End
5) Develop an algorithm, draw a flowchart that recommends travel destinations based on the user’s budget and preference
(Adventure or Relaxation). If the budget is over ₹1,50,000, recommend the Himalayas (for Adventure) or Paris (for
Relaxation). Otherwise, recommend Goa (for Relaxation) or Waynad (for Adventure).
Algorithm: Flowchart:
Step1: Start
Step2: Input: Read the budget value.
Step3: if budget value >$2000.
If preference is "Adventure"
print Recommend "New Zealand"
else
print Recommend "Europe"
Step4: if budget value <=$2000.
If preference is “Relaxation "
print Recommend " Thailand "
else
print Recommend " Domestic "
Step7: End
6) Develop a program that uses arithmetic operators to perform basic operations (addition, subtraction, multiplication,
division) and relational operators to compare two user-inputted numbers and display the results.
Program:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
if num2 != 0:
division = num1 / num2
else:
division = "undefined (division by zero)"
print("\nArithmetic Operations:")
print("Addition of two numbers", addition )
print("Subtraction of two numbers", subtraction)
print("Multiplication of two numbers", multiplication )
print("Division of two numbers",division)
print("\nRelational Operations:")
if num1 > num2:
print(num1, “is greater than”,num2)
elif num1 < num2:
print(num1, “is less than”,num2)
else:
print(num1, “is equal to”, num2)
7) Write an algorithm, and a Python program to check if a given year is a leap year. A year is a leap year if:
▪ It is divisible by 4,
▪ If divisible by 100, it must also be divisible by 400.
Algorithm: Program:
Step 1: Input: A year (integer). Year = int(input("Enter the Year: "))
Step 2: Check divisibility: if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):
▪ If the year is divisible by 400, then it print("Given Year is a leap Year");
is a leap year. else:
▪ If the year is divisible by 4 but not print ("Given Year is not a leap Year")
100, then it is a leap year Output:
▪ Else, it is not a leap year.
Enter the Year: 2024
Step 3: Output: Indicate whether the year is a Given Year is a leap Year
leap year or not.
Step 4: End
8) Write an algorithm, and a program that prints numbers from 1 to 10 using a while loop.
Algorithm: Program:
Step 1: Initialize a variable i to 1. i=1
Step 2: While i is less than or equal to 10: while i<=10:
• Print i. print(i, end=' ')
• Increment i by 1.
i+=1
Step 3: End Output: 1 2 3 4 5 6 7 8 9 10
9) Write an algorithm, and a program that categorizes a number as positive, negative, or zero.
Algorithm: Program:
Step 1: Input: Enter a number (let's call it num). numbers = input("Enter numbers separated by space: ").split()
Step 2: Check the value of num: for num in numbers:
num = float(num)
• If num is greater than 0:
o Print "Positive". if num > 0:
• Else if num is less than 0: print(f"The number {num} is positive because it is greater than zero.")
o Print "Negative". elif num < 0:
• Else: print(f"The number {num} is negative because it is less than zero.")
o Print "Zero". else:
Step 3: End
print(f"The number {num} is zero because it is neither positive nor
negative.")
10) Write an algorithm, and a program that prints a multiplication table up to 10 using nested loops.
Algorithm: Program:
Step 1: Start
Step 2: Initialize N to 10 (the size of the multiplication table). table_size = int(input("Enter the desired table size: "))
Step 3 :Outer Loop: for i in range(1, table_size + 1):
For i from 1 to N (inclusive): for j in range(1, table_size + 1):
1. Inner Loop:
▪ For j from 1 to N (inclusive):
product = i * j
1. Calculate product = i * j. print(f"{i} x {j} = {product}")
2. Print product with proper formatting print()
(e.g., aligned output).
2. Print a new line to move to the next row after the inner
loop completes.
Step 4: End