Virtual Lab File
Virtual Lab File
ON
SUBMITTED FOR
BACHELOR OF TECHNOLOGY
IN
(Session: 2024-2025)
1 Basic Calculator
5 Word Counter
8 Factorial Calculator
9 Temperature Converter
10 List Sorter
1. Basic Calculator
Code:
# Basic Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
result = num1 / num2 if num2 != 0 else "Undefined (division by zero)"
else:
result = "Invalid operator"
Output:
Input:
Enter first number: 10
Enter second number: 5
Enter operator (+, -, *, /): +
Output:
The result is: 15.0
2. Prime Number Checker
Code:
Output:
Input:
Enter a number: 7
Output:
7 is a prime number.
Input:
Enter a number: 8
Output:
8 is not a prime number.
3. Odd or Even Finder
Code:
Output:
Input:
Enter a number: 12
Output:
12 is even.
Input:
Enter a number: 7
Output:
7 is odd.
4. Fibonacci Series
Code:
# Fibonacci Series
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci Sequence:", end=" ")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
print()
Output:
Input:
Enter the number of terms: 6
Output:
Fibonacci Sequence: 0 1 1 2 3 5
5. Word Counter
Code:
# Word Counter
file_name = input("Enter the file name: ")
try:
with open(file_name, 'r') as file:
text = file.read()
lines = text.splitlines()
words = text.split()
characters = len(text)
print(f"Lines: {len(lines)}, Words: {len(words)}, Characters: {characters}")
except FileNotFoundError:
print("File not found.")
Output:
Input:
(File content in 'sample.txt')
Hello world.
This is a test file.
Output:
Lines: 2, Words: 7, Characters: 32
6. Simple Password Generator
Code:
import random
import string
def generate_password(length):
if length < 4: # Minimum length check
return "Password length must be at least 4 characters."
letters = string.ascii_letters
digits = string.digits
symbols = string.punctuation
# Ensure password contains at least one letter, one digit, and one symbol
all_characters = random.choice(letters) + random.choice(digits) +
random.choice(symbols)
all_characters += ''.join(random.choices(letters + digits + symbols, k=length-3))
Output:
Code:
import random
def number_guessing_game():
number_to_guess = random.randint(1, 100)
attempts = 0
print("Guess the number between 1 and 100!")
while True:
try:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < number_to_guess:
print("Too low!")
elif guess > number_to_guess:
print("Too high!")
else:
print(f"Congratulations! You guessed it in {attempts} attempts.")
break
except ValueError:
print("Please enter a valid number.")
number_guessing_game()
Output:
Example Output:
Guess the number between 1 and 100!
Enter your guess: 50
Too low!
Enter your guess: 75
Too high!
Enter your guess: 62
Congratulations! You guessed it in 3 attempts.
8. Factorial Calculator
Code:
def factorial(n):
if n < 0:
return "Factorial not defined for negative numbers."
result = 1
for i in range(1, n + 1):
result *= i
return result
number = 5
print(f"Factorial of {number} is {factorial(number)}")
Output:
Factorial of 5 is 120
9. Temperature Converter
Code:
def temperature_converter():
print("Temperature Converter")
temp = float(input("Enter the temperature: "))
unit = input("Enter the unit (C/F/K): ").upper()
if unit == "C":
print(f"{temp} Celsius is {temp * 9/5 + 32} Fahrenheit and {temp + 273.15}
Kelvin.")
elif unit == "F":
print(f"{temp} Fahrenheit is {(temp - 32) * 5/9} Celsius and {(temp - 32) * 5/9 +
273.15} Kelvin.")
elif unit == "K":
print(f"{temp} Kelvin is {temp - 273.15} Celsius and {(temp - 273.15) * 9/5 +
32} Fahrenheit.")
else:
print("Invalid unit.")
temperature_converter()
Output:
Example Conversion:
Enter the temperature: 25
Enter the unit (C/F/K): C
25 Celsius is 77.0 Fahrenheit and 298.15 Kelvin.
10. List Sorter
Code:
def list_sorter(numbers):
unique_numbers = list(set(numbers)) # Remove duplicates
ascending = sorted(unique_numbers)
descending = sorted(unique_numbers, reverse=True)
return ascending, descending
numbers = [3, 1, 2, 5, 3, 4, 1, 6]
ascending, descending = list_sorter(numbers)
print(f"Original list: {numbers}")
print(f"Ascending order: {ascending}")
print(f"Descending order: {descending}")
Output:
Example Conversion:
Enter the temperature: 25
Enter the unit (C/F/K): C
25 Celsius is 77.0 Fahrenheit and 298.15 Kelvin.