feat: add answers to all questions
This commit is contained in:
parent
9d0371d930
commit
0bb9a452a7
6
practical-file/answers/question_01.py
Normal file
6
practical-file/answers/question_01.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
# Question: Write a Python program to accept two integers and print their sum.
|
||||||
|
|
||||||
|
num1 = int(input("Enter first integer: "))
|
||||||
|
num2 = int(input("Enter second integer: "))
|
||||||
|
|
||||||
|
print(f"{num1} + {num2} = {num1 + num2}")
|
||||||
4
practical-file/answers/question_02.py
Normal file
4
practical-file/answers/question_02.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Question: Write a Python program that accepts the radius of a circle and prints its area.
|
||||||
|
|
||||||
|
radius = float(input("Enter the radius of the circle: "))
|
||||||
|
print(f"Area: {3.14 * radius * radius}")
|
||||||
9
practical-file/answers/question_03.py
Normal file
9
practical-file/answers/question_03.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# Question: Write a Python program to accept the length and width of a rectangle and compute its perimeter and area.
|
||||||
|
|
||||||
|
length = float(input("Enter the length of the rectangle: "))
|
||||||
|
width = float(input("Enter the width of the rectangle: "))
|
||||||
|
|
||||||
|
perimeter = 2 * (length + width)
|
||||||
|
area = length * width
|
||||||
|
print(f"Perimeter: {perimeter}")
|
||||||
|
print(f"Area: {area}")
|
||||||
8
practical-file/answers/question_04.py
Normal file
8
practical-file/answers/question_04.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Question: Write a Python program to compute simple interest for a given principal amount, time, and rate of interest.
|
||||||
|
|
||||||
|
amount = float(input("Enter the principal amount: "))
|
||||||
|
time = float(input("Enter the time in years: "))
|
||||||
|
rate = float(input("Enter the rate of interest: "))
|
||||||
|
|
||||||
|
simple_interest = (amount * time * rate) / 100
|
||||||
|
print(f"Simple Interest: {simple_interest}")
|
||||||
7
practical-file/answers/question_05.py
Normal file
7
practical-file/answers/question_05.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Question: Write a Python program to find whether a given number is even or odd.
|
||||||
|
|
||||||
|
num = int(input("Enter an integer: "))
|
||||||
|
if num % 2 == 0:
|
||||||
|
print(f"{num} is even")
|
||||||
|
else:
|
||||||
|
print(f"{num} is odd")
|
||||||
13
practical-file/answers/question_06.py
Normal file
13
practical-file/answers/question_06.py
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Question: Write a Python program to find the largest among three numbers.
|
||||||
|
|
||||||
|
num1 = float(input("Enter first number: "))
|
||||||
|
num2 = float(input("Enter second number: "))
|
||||||
|
num3 = float(input("Enter third number: "))
|
||||||
|
|
||||||
|
if num1 >= num2 and num1 >= num3:
|
||||||
|
largest = num1
|
||||||
|
elif num2 >= num1 and num2 >= num3:
|
||||||
|
largest = num2
|
||||||
|
else:
|
||||||
|
largest = num3
|
||||||
|
print(f"The largest number is: {largest}")
|
||||||
19
practical-file/answers/question_07.py
Normal file
19
practical-file/answers/question_07.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
# Question: Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
||||||
|
|
||||||
|
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 == '/':
|
||||||
|
if num2 != 0:
|
||||||
|
result = num1 / num2
|
||||||
|
else:
|
||||||
|
result = "Error: Division by zero"
|
||||||
|
else:
|
||||||
|
result = "Error: Invalid operator"
|
||||||
|
print(f"Result: {result}")
|
||||||
7
practical-file/answers/question_08.py
Normal file
7
practical-file/answers/question_08.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Question: Write a Python program to check whether a given year is a leap year or not.
|
||||||
|
|
||||||
|
year = int(input("Enter a year: "))
|
||||||
|
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
|
||||||
|
print(f"{year} is a leap year")
|
||||||
|
else:
|
||||||
|
print(f"{year} is not a leap year")
|
||||||
9
practical-file/answers/question_09.py
Normal file
9
practical-file/answers/question_09.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# Question: Write a Python program to check if a number is positive, negative, or zero.
|
||||||
|
|
||||||
|
num = float(input("Enter a number: "))
|
||||||
|
if num > 0:
|
||||||
|
print(f"{num} is positive")
|
||||||
|
elif num < 0:
|
||||||
|
print(f"{num} is negative")
|
||||||
|
else:
|
||||||
|
print(f"{num} is zero")
|
||||||
19
practical-file/answers/question_10.py
Normal file
19
practical-file/answers/question_10.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
"""
|
||||||
|
Question: Write a Python program to accept marks of a student and print the grade according to the given criteria:
|
||||||
|
Marks ≥ 90: A
|
||||||
|
Marks ≥ 80: B
|
||||||
|
Marks ≥ 70: C
|
||||||
|
Marks ≥ 60: D
|
||||||
|
Otherwise: Fail
|
||||||
|
"""
|
||||||
|
marks = float(input("Enter the marks of the student: "))
|
||||||
|
if marks >= 90:
|
||||||
|
print("Grade: A")
|
||||||
|
elif marks >= 80:
|
||||||
|
print("Grade: B")
|
||||||
|
elif marks >= 70:
|
||||||
|
print("Grade: C")
|
||||||
|
elif marks >= 60:
|
||||||
|
print("Grade: D")
|
||||||
|
else:
|
||||||
|
print("Grade: Fail")
|
||||||
10
practical-file/answers/question_11.py
Normal file
10
practical-file/answers/question_11.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Question: Write a Python program to check whether a given character is a vowel or consonant.
|
||||||
|
|
||||||
|
char = input("Enter a character: ").lower()
|
||||||
|
if char.isalpha() and len(char) == 1:
|
||||||
|
if char in 'aeiou':
|
||||||
|
print(f"{char} is a vowel")
|
||||||
|
else:
|
||||||
|
print(f"{char} is a consonant")
|
||||||
|
else:
|
||||||
|
print("Please enter a valid single alphabet character.")
|
||||||
7
practical-file/answers/question_12.py
Normal file
7
practical-file/answers/question_12.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Question: Write a Python program to check whether a number is divisible by both 5 and 11.
|
||||||
|
|
||||||
|
num = int(input("Enter an integer: "))
|
||||||
|
if num % 5 == 0 and num % 11 == 0:
|
||||||
|
print(f"{num} is divisible by both 5 and 11")
|
||||||
|
else:
|
||||||
|
print(f"{num} is not divisible by both 5 and 11")
|
||||||
4
practical-file/answers/question_13.py
Normal file
4
practical-file/answers/question_13.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Question: Write a Python program to find the absolute value of a number.
|
||||||
|
|
||||||
|
num = float(input("Enter a number: "))
|
||||||
|
print(f"The absolute value of {num} is: {abs(num)}")
|
||||||
10
practical-file/answers/question_14.py
Normal file
10
practical-file/answers/question_14.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Question: Write a Python program to check if a character is an uppercase or lowercase letter.
|
||||||
|
|
||||||
|
char = input("Enter a character: ")
|
||||||
|
if char.isalpha() and len(char) == 1:
|
||||||
|
if char.isupper():
|
||||||
|
print(f"{char} is an uppercase letter")
|
||||||
|
else:
|
||||||
|
print(f"{char} is a lowercase letter")
|
||||||
|
else:
|
||||||
|
print("Please enter a valid single alphabet character.")
|
||||||
7
practical-file/answers/question_15.py
Normal file
7
practical-file/answers/question_15.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Question: Write a Python program to accept age and check whether the person is eligible to vote (18 or older).
|
||||||
|
|
||||||
|
age = int(input("Enter your age: "))
|
||||||
|
if age >= 18:
|
||||||
|
print("You are eligible to vote.")
|
||||||
|
else:
|
||||||
|
print("You are not eligible to vote.")
|
||||||
10
practical-file/answers/question_16.py
Normal file
10
practical-file/answers/question_16.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Question: Write a Python program to accept three sides and check whether they form a valid triangle.
|
||||||
|
|
||||||
|
side1 = float(input("Enter the length of the first side: "))
|
||||||
|
side2 = float(input("Enter the length of the second side: "))
|
||||||
|
side3 = float(input("Enter the length of the third side: "))
|
||||||
|
|
||||||
|
if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
|
||||||
|
print("The sides form a valid triangle.")
|
||||||
|
else:
|
||||||
|
print("The sides do not form a valid triangle.")
|
||||||
11
practical-file/answers/question_17.py
Normal file
11
practical-file/answers/question_17.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Question: Write a Python program to accept cost price and selling price, then print whether there is profit, loss, or no gain.
|
||||||
|
|
||||||
|
cost_price = float(input("Enter the cost price: "))
|
||||||
|
selling_price = float(input("Enter the selling price: "))
|
||||||
|
|
||||||
|
if selling_price > cost_price:
|
||||||
|
profit = selling_price - cost_price
|
||||||
|
print(f"There is a profit of: {profit}")
|
||||||
|
elif selling_price < cost_price:
|
||||||
|
loss = cost_price - selling_price
|
||||||
|
print(f"There is a loss of: {loss}")
|
||||||
9
practical-file/answers/question_18.py
Normal file
9
practical-file/answers/question_18.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# Question: Write a Python program to accept two numbers and check whether they are equal or not.
|
||||||
|
|
||||||
|
num1 = int(input("Enter first number: "))
|
||||||
|
num2 = int(input("Enter second number: "))
|
||||||
|
|
||||||
|
if num1 == num2:
|
||||||
|
print(f"{num1} and {num2} are equal.")
|
||||||
|
else:
|
||||||
|
print(f"{num1} and {num2} are not equal.")
|
||||||
|
|
@ -0,0 +1,185 @@
|
||||||
|
Question 01: Write a Python program to accept two integers and print their sum.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num1 = int(input("Enter first integer: "))
|
||||||
|
num2 = int(input("Enter second integer: "))
|
||||||
|
|
||||||
|
print(f"{num1} + {num2} = {num1 + num2}")Code execution timed out
|
||||||
|
|
||||||
|
Question 02: Write a Python program that accepts the radius of a circle and prints its area.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
radius = float(input("Enter the radius of the circle: "))
|
||||||
|
print(f"Area: {3.14 * radius * radius}")
|
||||||
|
|
||||||
|
Question 03: Write a Python program to accept the length and width of a rectangle and compute its perimeter and area.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
length = float(input("Enter the length of the rectangle: "))
|
||||||
|
width = float(input("Enter the width of the rectangle: "))
|
||||||
|
|
||||||
|
perimeter = 2 * (length + width)
|
||||||
|
area = length * width
|
||||||
|
print(f"Perimeter: {perimeter}")
|
||||||
|
print(f"Area: {area}")Code execution timed out
|
||||||
|
|
||||||
|
Question 04: Write a Python program to compute simple interest for a given principal amount, time, and rate of interest.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
amount = float(input("Enter the principal amount: "))
|
||||||
|
time = float(input("Enter the time in years: "))
|
||||||
|
rate = float(input("Enter the rate of interest: "))
|
||||||
|
|
||||||
|
simple_interest = (amount * time * rate) / 100
|
||||||
|
print(f"Simple Interest: {simple_interest}")
|
||||||
|
|
||||||
|
Question 05: Write a Python program to find whether a given number is even or odd.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num = int(input("Enter an integer: "))
|
||||||
|
if num % 2 == 0:
|
||||||
|
print(f"{num} is even")
|
||||||
|
else:
|
||||||
|
print(f"{num} is odd")
|
||||||
|
|
||||||
|
Question 06: Write a Python program to find the largest among three numbers.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num1 = float(input("Enter first number: "))
|
||||||
|
num2 = float(input("Enter second number: "))
|
||||||
|
num3 = float(input("Enter third number: "))
|
||||||
|
|
||||||
|
if num1 >= num2 and num1 >= num3:
|
||||||
|
largest = num1
|
||||||
|
elif num2 >= num1 and num2 >= num3:
|
||||||
|
largest = num2
|
||||||
|
else:
|
||||||
|
largest = num3
|
||||||
|
print(f"The largest number is: {largest}")
|
||||||
|
|
||||||
|
|
||||||
|
Question 07: Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
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 == '/':
|
||||||
|
if num2 != 0:
|
||||||
|
result = num1 / num2
|
||||||
|
else:
|
||||||
|
result = "Error: Division by zero"
|
||||||
|
else:
|
||||||
|
result = "Error: Invalid operator"
|
||||||
|
print(f"Result: {result}")
|
||||||
|
|
||||||
|
Question 08: Write a Python program to check whether a given year is a leap year or not.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
year = int(input("Enter a year: "))
|
||||||
|
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
|
||||||
|
print(f"{year} is a leap year")
|
||||||
|
else:
|
||||||
|
print(f"{year} is not a leap year")
|
||||||
|
|
||||||
|
Question 09: Write a Python program to check if a number is positive, negative, or zero.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num = float(input("Enter a number: "))
|
||||||
|
if num > 0:
|
||||||
|
print(f"{num} is positive")
|
||||||
|
elif num < 0:
|
||||||
|
print(f"{num} is negative")
|
||||||
|
else:
|
||||||
|
print(f"{num} is zero")
|
||||||
|
|
||||||
|
Question 11: Write a Python program to accept marks of a student and print the grade according to the given criteria:
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num = float(input("Enter a number: "))
|
||||||
|
if num > 0:
|
||||||
|
print(f"{num} is positive")
|
||||||
|
elif num < 0:
|
||||||
|
print(f"{num} is negative")
|
||||||
|
else:
|
||||||
|
print(f"{num} is zero")
|
||||||
|
|
||||||
|
Question 11: Write a Python program to check whether a given character is a vowel or consonant.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
char = input("Enter a character: ").lower()
|
||||||
|
if char.isalpha() and len(char) == 1:
|
||||||
|
if char in 'aeiou':
|
||||||
|
print(f"{char} is a vowel")
|
||||||
|
else:
|
||||||
|
print(f"{char} is a consonant")
|
||||||
|
else:
|
||||||
|
print("Please enter a valid single alphabet character.")
|
||||||
|
|
||||||
|
|
||||||
|
Question 12: Write a Python program to check whether a number is divisible by both 5 and 11.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num = int(input("Enter an integer: "))
|
||||||
|
if num % 5 == 0 and num % 11 == 0:
|
||||||
|
print(f"{num} is divisible by both 5 and 11")
|
||||||
|
else:
|
||||||
|
print(f"{num} is not divisible by both 5 and 11")
|
||||||
|
|
||||||
|
Question 13: Write a Python program to find the absolute value of a number.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
num = float(input("Enter a number: "))
|
||||||
|
print(f"The absolute value of {num} is: {abs(num)}")
|
||||||
|
|
||||||
|
Question 14: Write a Python program to check if a character is an uppercase or lowercase letter.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
char = input("Enter a character: ")
|
||||||
|
if char.isalpha() and len(char) == 1:
|
||||||
|
if char.isupper():
|
||||||
|
print(f"{char} is an uppercase letter")
|
||||||
|
else:
|
||||||
|
print(f"{char} is a lowercase letter")
|
||||||
|
else:
|
||||||
|
print("Please enter a valid single alphabet character.")
|
||||||
|
|
||||||
|
Question 15: Write a Python program to accept age and check whether the person is eligible to vote (18 or older).
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
age = int(input("Enter your age: "))
|
||||||
|
if age >= 18:
|
||||||
|
print("You are eligible to vote.")
|
||||||
|
else:
|
||||||
|
print("You are not eligible to vote.")
|
||||||
|
|
||||||
|
Question 16: Write a Python program to accept three sides and check whether they form a valid triangle.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
side1 = float(input("Enter the length of the first side: "))
|
||||||
|
side2 = float(input("Enter the length of the second side: "))
|
||||||
|
side3 = float(input("Enter the length of the third side: "))
|
||||||
|
|
||||||
|
if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
|
||||||
|
print("The sides form a valid triangle.")
|
||||||
|
else:
|
||||||
|
print("The sides do not form a valid triangle.")
|
||||||
|
|
||||||
|
Question 17: Write a Python program to accept cost price and selling price, then print whether there is profit, loss, or no gain.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
cost_price = float(input("Enter the cost price: "))
|
||||||
|
selling_price = float(input("Enter the selling price: "))
|
||||||
|
|
||||||
|
if selling_price > cost_price:
|
||||||
|
profit = selling_price - cost_price
|
||||||
|
print(f"There is a profit of: {profit}")
|
||||||
|
elif selling_price < cost_price:
|
||||||
|
loss = cost_price - selling_price
|
||||||
|
print(f"There is a loss of: {loss}")
|
||||||
|
|
@ -73,7 +73,7 @@ def read_code_from_file(filename):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Error reading file: {str(e)}"
|
return f"Error reading file: {str(e)}"
|
||||||
|
|
||||||
def append_to_master_file(question_text, question_num, code_content, execution_output):
|
def append_to_master_file(question_text, question_num, code_content):
|
||||||
with open("outputs.txt", "a") as f:
|
with open("outputs.txt", "a") as f:
|
||||||
if os.path.getsize("outputs.txt") == 0:
|
if os.path.getsize("outputs.txt") == 0:
|
||||||
pass
|
pass
|
||||||
|
|
@ -84,17 +84,6 @@ def append_to_master_file(question_text, question_num, code_content, execution_o
|
||||||
f.write("Answer:\n")
|
f.write("Answer:\n")
|
||||||
f.write(code_content)
|
f.write(code_content)
|
||||||
|
|
||||||
if execution_output['returncode'] == 0:
|
|
||||||
if execution_output['stdout']:
|
|
||||||
f.write(execution_output['stdout'])
|
|
||||||
else:
|
|
||||||
f.write("No output")
|
|
||||||
else:
|
|
||||||
if execution_output['stderr']:
|
|
||||||
f.write(execution_output['stderr'])
|
|
||||||
else:
|
|
||||||
f.write("Error occurred")
|
|
||||||
|
|
||||||
def create_output_file(question_text, question_num, filename, code_content, execution_output):
|
def create_output_file(question_text, question_num, filename, code_content, execution_output):
|
||||||
output_filename = f"question_{question_num:02d}_output.txt"
|
output_filename = f"question_{question_num:02d}_output.txt"
|
||||||
|
|
||||||
|
|
@ -130,23 +119,23 @@ def main():
|
||||||
wait_for_completion()
|
wait_for_completion()
|
||||||
|
|
||||||
code_content = read_code_from_file(filename)
|
code_content = read_code_from_file(filename)
|
||||||
execution_output = execute_code_and_capture_output(filename)
|
# execution_output = execute_code_and_capture_output(filename)
|
||||||
|
|
||||||
create_output_file(question, question_num, filename, code_content, execution_output)
|
# create_output_file(question, question_num, filename, code_content, execution_output)
|
||||||
append_to_master_file(question, question_num, code_content, execution_output)
|
append_to_master_file(question, question_num, code_content)
|
||||||
|
|
||||||
if execution_output['returncode'] == 0:
|
# if execution_output['returncode'] == 0:
|
||||||
if execution_output['stdout']:
|
# if execution_output['stdout']:
|
||||||
print("Output:")
|
# print("Output:")
|
||||||
print(execution_output['stdout'])
|
# print(execution_output['stdout'])
|
||||||
else:
|
# else:
|
||||||
print("No output")
|
# print("No output")
|
||||||
else:
|
# else:
|
||||||
print("Error:")
|
# print("Error:")
|
||||||
if execution_output['stderr']:
|
# if execution_output['stderr']:
|
||||||
print(execution_output['stderr'])
|
# print(execution_output['stderr'])
|
||||||
else:
|
# else:
|
||||||
print("Error occurred")
|
# print("Error occurred")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
while True:
|
while True:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue