DELHI PUBLIC SCHOOL RAU
INDORE
AISSCE PHYSICS PROJECT 2023-2024
Computer Science Practical File
“20 Programs And Their Outputs”
Session 2023-24
Submitted to: Submitted by:
Mr. Ankur Luhadiya Naman Gupta
XI – A
[Link] a python program to accept two integers
and print their sum.
CODE:
# Accept two integers from the user
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
# Calculate the sum
sum_result = num1 + num2
# Print the result
print(f"The sum of {num1} and {num2} is:{sum_result}")
OUTPUT:
Enter the first integer: 60
Enter the second integer: 70
The sum of 60 and 70 is: 130
Q2: Write a python program that accepts radius
of a circle and prints its area?
CODE:
import math
# Accept the radius from the user
radius = float(input("Enter the radius of the circle: "))
# Calculate the area of the circle using the formula: area = π * r^2
area = [Link] * radius**2
# Print the result
print(f"The area of the circle with radius {radius} is: {area:.2f}")
OUTPUT:
Enter the radius of the circle: 5
The area of the circle with radius 5.0 is: 78.53
Q3: Write a python program to accept length and
width of a rectangle and compute its perimeter
and area.
CODE:
# Accept the length and width from the user
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculate the perimeter and area of the rectangle
perimeter = 2 * (length + width)
area = length * width
# Print the results
print(f"The perimeter of the rectangle is: {perimeter}")
print(f"The area of the rectangle is: {area}")
OUTPUT :
Enter the length of the rectangle: 10
Enter the width of the rectangle: 20
The perimeter of the rectangle is: 60.0
The area of the rectangle is: 200.0
Q4: Write a python program to computer simple
interest for given Principal amount, time and rate
of interest.
CODE:
# Function to calculate simple interest
def calculate_simple_interest(principal, rate, time):
interest = (principal * rate * time) / 100
return interest
# Accept input from the user
principal_amount = float(input("Enter the principal amount: "))
rate_of_interest = float(input("Enter the rate of interest (per
annum): "))
time_in_years = float(input("Enter the time (in years): "))
# Calculate simple interest using the function
simple_interest = calculate_simple_interest(principal_amount,
rate_of_interest, time_in_years)
# Print the result
print(f"\nThe Simple Interest for Principal ${principal_amount},
Rate {rate_of_interest}% per annum, and Time {time_in_years}
years is: ${simple_interest:.2f}")
OUTPUT :
Enter the principal amount: 10000
Enter the rate of interest (per annum): 8
Enter the time (in years): 5
The Simple Interest for Principal $5000.0, Rate 8.0% per annum,
and Time 5.0 years is: $4000.00
Q5: Write a python program to find whether a
given number is even or odd?
CODE:
# Function to check if a number is even or odd
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else: return "Odd"
# Accept input from the user
user_number = int(input("Enter a number: "))
# Check if the number is even or odd using the function
result = check_even_odd(user_number)
# Print the result
print(f"The number {user_number} is {result}.")
OUTPUT :
Enter a number: 10
The number 10 is Even.
Q6: Write a python program to find largest
among three numbers?
CODE:
# Function to find the largest among three numbers
def find_largest(num1, num2, num3):
return max(num1, num2, num3)
# Accept input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Find the largest number using the function
largest_number = find_largest(num1, num2, num3)
# Print the result
print(f"The largest number among {num1}, {num2}, and
{num3} is: {largest_number}
OUTPUT :
Enter the first number: 20
Enter the second number: 23
Enter the third number: 19
The largest number among 20.0, 23.0, and 19.0 is: 23.0
Q7. Write a python program to perform
arithmetic calculation. The program accepts two
operands and an operator then displays the
calculated result.
CODE:
# Function to perform arithmetic calculation
def calculate_result(operand1, operand2, operator):
if operator == '+':
return operand1 + operand2
elif operator == '-':
return operand1 - operand2
elif operator == '*':
return operand1 * operand2
elif operator == '/':
if operand2 != 0:
return operand1 / operand2
else:
return "Error: Division by zero"
else:
return "Error: Invalid operator"
# Accept input from the user
operand1 = float(input("Enter the first operand: "))
operand2 = float(input("Enter the second operand: "))
operator = input("Enter the operator (+, -, *, /): ")
# Perform the arithmetic calculation using the function
result = calculate_result(operand1, operand2, operator)
# Print the result
print(f"The result of {operand1} {operator} {operand2} is:
{result}")
OUTPUT :
Enter the first operand: 80
Enter the second operand: 70
Enter the operator (+, -, *, /): +
The result of 80.0 + 70.0 is: 150.0
Q8: Write a python program to check whether a
given year is a leap year or not?
CODE:
# Function to check if a year is a leap year
def is_leap_year(year):
# Leap years are divisible by 4, but not divisible by 100 unless
divisible by 400
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
# Accept input from the user
year = int(input("Enter a year: "))
# Check if the year is a leap year using the function
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
OUTPUT:
Enter a year: 2024
2024 is a leap year.
Enter a year: 1969
1969 is not a leap year.
Q9: Write a python to print table of a given
number?
CODE:
# Function to print the table of a given number
def print_table(number):
print(f"Table of {number}:")
for i in range(1, 11):
result = number * i
print(f"{number} x {i} = {result}")
# Accept input from the user
number = int(input("Enter a number to print its table: "))
# Print the table using the function
print_table(number)
OUTPUT:
Enter a number to print its table: 9
Table of 9:
9x1=9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
Q10: Write a python program to print first N
natural numbers and their sum?
CODE:
# Function to print first N natural numbers and their sum
def print_natural_numbers_and_sum(N):
if N <= 0:
print("Please enter a positive integer.")
return
# Print the first N natural numbers
print(f"First {N} natural numbers:")
for i in range(1, N + 1):
print(i, end=' ')
# Calculate and print the sum of the first N natural numbers
sum_of_numbers = (N * (N + 1)) // 2
print(f"\nSum of the first {N} natural numbers
is:{sum_of_numbers}")
# Accept input from the user
N = int(input("Enter a positive integer (N): "))
# Call the function to print natural numbers and their sum
print_natural_numbers_and_sum(N)
OUTPUT
Enter a positive integer (N): 20
First 20 natural numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sum of the first 20 natural numbers is: 210
Q11: Write a python program to accept two
integers X and N and compute XN.
CODE:
X = int(input("Enter the value of X :"))
N = int(input("Enter the value of N : "))
print(f"The Value of XN is : {10*X+N}")
OUTPUT:
Enter the value of X : 1
Enter the value of N : 2
The Value of XN is : 12
Enter the value of X : 9
Enter the value of N :8
The Value of XN is : 98
Q12: Write a python program to calculate
factorial of a given number using while loop?
CODE:
# Function to calculate factorial using while loop
def calculate_factorial(number):
if number < 0:
return "Factorial is not defined for negative numbers."
elif number == 0 or number == 1:
return 1
else:
result = 1
while number > 1:
result *= number
number -= 1
return result
# Accept input from the user
num = int(input("Enter a non-negative integer to calculate its
factorial: "))
# Calculate factorial using the function
factorial_result = calculate_factorial(num)
# Print the result
print(f"The factorial of {num} is: {factorial_result}")
OUTPUT
Enter a non-negative integer to calculate its factorial: 6
The factorial of 6 is: 720
Q13: Write a program to print fibonacci series?
CODE:
# Function to generate Fibonacci series up to N terms
def generate_fibonacci_series(N):
fib_series = []
a, b = 0, 1
while len(fib_series) < N:
fib_series.append(a) a, b = b, a + b
return fib_series
# Accept input from the user
num_terms = int(input("Enter the number of terms for the
Fibonacci series: "))
# Generate and print the Fibonacci series using the function
fibonacci_series = generate_fibonacci_series(num_terms)
print(f"Fibonacci series up to {num_terms} terms:
{fibonacci_series}
OUTPUT:
Enter the number of terms for the Fibonacci series: 10
Fibonacci series up to 10 terms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Q14: Write a program to check whether a given
number is equal to sum of the cubes of its digits?
CODE:
# Function to check if a number is equal to the sum of cubes of
its digits
def is_equal_to_sum_of_cubes(number):
original_number = number sum_of_cubes = 0
# Calculate the sum of cubes of digits
while number > 0:
digit = number % 10
sum_of_cubes += digit ** 3
number //= 10
# Check if the original number is equal to the sum of cubes return
original_number == sum_of_cubes
# Accept input from the user
num = int(input("Enter a number to check: "))
# Check if the number is equal to the sum of cubes using the
function
result = is_equal_to_sum_of_cubes(num)
# Print the result
if result:
print(f"{num} is equal to the sum of cubes of its digits.")
else: print(f"{num} is not equal to the sum of cubes of its
digits.")
OUTPUT:
Enter a number to check: 999
999 is not equal to the sum of cubes of its digits.
Enter a number to check: 153
153 is equal to the sum of cubes of its digits.
Q15: Write a program to print following
pattern on screen.
*
**
***
****
*****
CODE:
# Function to print the specified pattern
def print_pattern():
for i in range(1, 6):
print('*' * i)
# Call the function to print the pattern print_pattern()
print_pattern()
OUTPUT:
*
**
***
****
*****
Q16: Compute the greatest common divisor and
least common multiple of two integers?
CODE:
# Function to compute the greatest common divisor (GCD)
def compute_gcd(a, b):
while b: a, b = b, a % b
return a
# Function to compute the least common multiple (LCM)
def compute_lcm(a, b):
return abs(a * b) // compute_gcd(a, b)
# Accept input from the user
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
# Compute GCD and LCM using the functions
gcd_result = compute_gcd(num1, num2)
lcm_result = compute_lcm(num1, num2)
# Print the results
print(f"The GCD of {num1} and {num2} is: {gcd_result}")
print(f"The LCM of {num1} and {num2} is: {lcm_result}")
OUTPUT:
Enter the first integer: 100
Enter the second integer: 200
The GCD of 100 and 200 is: 100
The LCM of 100 and 200 is: 200
Q17: Write a program to generate prime numbers
for a given range?
CODE:
# Function to check if a number is prime
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# Function to generate prime numbers in a given range
def generate_primes_in_range(start, end):
prime_numbers = [num for num in range(start, end + 1) if
is_prime(num)]
return prime_numbers
# Accept input from the user
start_range = int(input("Enter the starting range: "))
end_range = int(input("Enter the ending range: "))
# Generate and print prime numbers in the given range
prime_numbers_in_range
=generate_primes_in_range(start_range, end_range)
print(f"Prime numbers in the range [{start_range}, {end_range}]:
{prime_numbers_in_range}")
OUPUT:
Enter the starting range: 4
Enter the ending range: 20
Prime numbers in the range [4, 20]: [5,7,11,13,17,19]
Q18: Write a program to check whether a given
string is a palindrome or not?
CODE:
#Function to check if a string is a palindrome
def is_palindrome(input_str):
cleaned_str = ''.join([Link]() for char in input_str if
[Link]())
return cleaned_str == cleaned_str[::-1]
# Accept input from the user
user_input = input("Enter a string to check for palindrome: ")
# Check if the string is a palindrome using the function
if is_palindrome(user_input):
print(f"{user_input} is a palindrome.")
else: print(f"{user_input} is not a palindrome.")
OUTPUT:
Enter a string to check for palindrome: carrace
carrace is not a palindrome.
Enter a string to check for palindrome: racecar
racecar is a palindrome.
Q19: Write a program to sort a list of 10 numbers
using bubble sort method?
CODE:
# Function to perform bubble sort on a list
def bubble_sort(numbers):
n = len(numbers)
for i in range(n - 1): for j in range(0, n - i - 1):
if numbers[j] > numbers[j + 1]:
# Swap if the element found is greater than the next element
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
# Accept input from the user for a list of 10 numbers
numbers_list = [int(input(f"Enter number {i + 1}: ")) for i in
range(10)] # Perform bubble sort on the list
bubble_sort(numbers_list)
# Print the sorted list
print("Sorted list:", numbers_list)
Enter number 1: 92
Enter number 2: 11
Enter number 3: 37
Enter number 4: 12
Enter number 5: 992
Enter number 6: 54
Enter number 7: 32
Enter number 8: 22
Enter number 9: 59
Enter number 10: 86
Sorted list: [11, 12, 22, 32, 37, 54, 59, 86, 92, 992]
Q20: Write a program to sort a list of 10 numbers
using insertion sort method?
CODE:
# Function to perform insertion sort on a list
def insertion_sort(numbers):
for i in range(1, len(numbers)):
key = numbers[i]
j=i-1
while
j >= 0 and key < numbers[j]:
numbers[j + 1] = numbers[j]
j -= 1
numbers[j + 1] = key
# Accept input from the user for a list of 10 numbers
numbers_list = [int(input(f"Enter number {i + 1}: ")) for i in
range(10)]
# Perform insertion sort on the list
insertion_sort(numbers_list)
# Print the sorted
list print("Sorted list:", numbers_list)
Enter number 1: 22
Enter number 2: 67
Enter number 3: 41
Enter number 4: 11
Enter number 5: 66
Enter number 6: 87
Enter number 7: 45
Enter number 8: 62
Enter number 9: 34
Enter number 10: 74
Sorted list: [11, 22, 28, 34, 41, 45, 62, 67, 74, 87]