Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

python lab

Uploaded by

srujankumarb4u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

python lab

Uploaded by

srujankumarb4u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Write an algorithm, draw a flowchart to swap the values of two variables with and without using a third variable.

Example: Given A = 5 and B = 10, after the swap, A = 10 and B = 5.


Algorithm: Flowchart:
Step 1: Start
Step 2: Input: Read values for a and b.
Step 3: Assign Temp: temp = a
Step 4: Assign: a = b
Step 5: Assign: b = temp
Step 6: Output: Display the swapped values of a and b.
Step 7: End
2) Create an algorithm, draw a flowchart that calculates the sum of a given set of numbers. Example: Given the numbers
12, 15, 7, 10, the program should compute their total sum: 44.
Algorithm: Flowchart:
Step 1: Start
Step 2: Input:
• Read the first number and assign to a.
• Read the second number and assign to b.
• Read the third number and assign to c.
• Read the fourth number and assign to d.
Step 3: Compute Sum:
• Calculate the sum of a, b, c, and d.
sum = a+b+c+d

• Store the result in a variable called sum.


Step 4: Output:
• Display or print the value of sum
Step 5: End
3) Develop an algorithm, draw a flowchart to reverse the digits of an integer. Example: If the input is 12345, the output
should be 54321.
Algorithm: Flowchart:
Step1: Start
Step 2: Input the integer N.
Step 3: Initialize reversed number to 0.
Step 4: While N is not 0:

• Extract the last digit of N using digit = N


% 10.
• Update reversed number to reversed
number * 10 + digit.
• Remove the last digit from N by
performing integer division N = N // 10.

Step 5: Output: print reversed number.


Step 6: End
4) Write an algorithm, draw a flowchart that generates the first ‘n’ terms of the Fibonacci sequence. Example: Generate the
first 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

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:

While counter is less than or equal to n:

o Output a (print the current term).


o Compute c as a + b.
o Update a to b.
o Update b to c.
o Increment counter i by 1.

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

You might also like