TECHNO INDIA GROUP PUBLIC SCHOOL
NAME: SATWIK BHATTACHARJEE
CLASS: X SECTION: E ROLL: 32
SUBJECT: ARTIFICIAL INTELLIGENCE
TOPIC: Python Programming
SESSION: 2024-2025
CONTENTS
1. Input an integer and check whether it is a Prime or Composite
number.
2. Input a number and find the sum of the digits.
3. Find a number is whether Palindrome or not. A palindromic number
(also known as a numeral palindrome or a numeric palindrome) is a
number (such as 16461) that remains the same when its digits are
reversed.
4. Print the Fibonacci series for a given number of terms. Fibonacci
sequence is a sequence in which each number is the sum of the two
preceding ones. Starting from 0 and 1, the sequence is as follows: 0, 1,
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144....
5. Input a number and check whether it is Perfect or not. A perfect
number is a positive integer that is equal to the sum of its positive
proper divisors, that is, divisors excluding the number itself. For
instance, 6 has proper divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a
perfect number.
6. Enter the principal amount, rate of interest, time and frequency of
interest compounded annually, then find the compound interest.
7. Input the base and height of a right-angled triangle and find the
hypotenuse.
8. Input a list of numbers and print the product of it.
9. Input a list of numbers and print the alternate elements.
10. Input N numbers and add it in a list. Then find the average of
elements.
Q: Write the program to Input an integer and check whether it is
a Prime or Composite number.
Soln:
def check_prime_or_composite(num):
if num <= 1:
return "Neither prime nor composite"
elif num == 2:
return "Prime"
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return "Composite"
return "Prime"
# Input from the user
try:
number = int(input("Enter an integer: "))
result = check_prime_or_composite(number)
print(f"The number {number} is: {result}")
except ValueError:
print("Please enter a valid integer.")
RESULT:
Q: Write the program to Input a number and find the sum of the
digits.
Soln:
def sum_of_digits(num):
# Initialize sum to 0
total = 0
# Convert the number to a string to iterate over each digit
for digit in str(abs(num)): # Use abs() to handle negative
numbers
total += int(digit) # Convert each character back to an
integer and add to the total
return total
# Input from the user
try:
number = int(input("Enter a number: "))
result = sum_of_digits(number)
print(f"The sum of the digits in {number} is: {result}")
except ValueError:
print("Please enter a valid integer.")
RESULT:
Q: Write the program to Find a number is whether Palindrome or
not. A palindromic number (also known as a numeral palindrome
or a numeric palindrome) is a number (such as 16461) that remains
the same when its digits are reversed.
Soln:
def is_palindrome(num):
# Convert the number to a string
num_str = str(num)
# Check if the string is equal to its reverse
return num_str == num_str[::-1]
# Input from the user
try:
number = int(input("Enter a number: "))
if is_palindrome(number):
print(f"The number {number} is a palindrome.")
else:
print(f"The number {number} is not a palindrome.")
except ValueError:
print("Please enter a valid integer.")
RESULT:
Q: Write the program to Print the Fibonacci series for a given
number of terms. Fibonacci sequence is a sequence in which each
number is the sum of the two preceding ones. Starting from 0 and
1, the sequence is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
144....
Soln:
def fibonacci_series(n):
# Initialize the first two Fibonacci numbers
fib_sequence = [0, 1]
# Generate Fibonacci series up to n terms
for i in range(2, n):
next_term = fib_sequence[i - 1] + fib_sequence[i - 2]
fib_sequence.append(next_term)
return fib_sequence[:n] # Return only the requested number
of terms
# Input from the user
try:
terms = int(input("Enter the number of terms in the
Fibonacci series: "))
if terms <= 0:
print("Please enter a positive integer.")
else:
result = fibonacci_series(terms)
print(f"The Fibonacci series for {terms} terms is:
{result}")
except ValueError:
print("Please enter a valid integer.")
RESULT:
Q: Input a number and check whether it is Perfect or not. A perfect
number is a positive integer that is equal to the sum of its positive
proper divisors, that is, divisors excluding the number itself. For
instance, 6 has proper divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is
a perfect number.
Soln:
def is_perfect_number(num):
# A perfect number is greater than 1
if num < 1:
return False
# Calculate the sum of proper divisors
sum_of_divisors = 0
for i in range(1, num):
if num % i == 0:
sum_of_divisors += i
# Check if the sum of divisors equals the original number
return sum_of_divisors == num
# Input from the user
try:
number = int(input("Enter a positive integer: "))
if is_perfect_number(number):
print(f"The number {number} is a perfect number.")
else:
print(f"The number {number} is not a perfect number.")
except ValueError:
print("Please enter a valid positive integer.")
RESULT:
Q: Write a program to Enter the principal amount, rate of interest,
time and frequency of interest compounded annually, then find the
compound interest.
Soln:
def calculate_compound_interest(principal, rate, time, frequency):
# Calculate compound interest
amount = principal * (1 + rate / (100 * frequency)) ** (frequency *
time)
compound_interest = amount - principal
return compound_interest
# Input from the user
try:
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest (in %): "))
time = float(input("Enter the time (in years): "))
frequency = int(input("Enter the number of times interest is
compounded per year: "))
# Calculate compound interest
interest = calculate_compound_interest(principal, rate, time,
frequency)
print(f"The compound interest is: {interest:.2f}")
except ValueError:
print("Please enter valid numeric values.")
RESULT:
Q: Write the program to Input the base and height of a right-
angled triangle and find the hypotenuse.
Soln:
import math
def calculate_hypotenuse(base, height):
# Use the Pythagorean theorem to calculate the hypotenuse
hypotenuse = math.sqrt(base**2 + height**2)
return hypotenuse
# Input from the user
try:
base = float(input("Enter the base of the right-angled triangle: "))
height = float(input("Enter the height of the right-angled triangle:
"))
# Calculate the hypotenuse
hypotenuse = calculate_hypotenuse(base, height)
print(f"The hypotenuse of the triangle is: {hypotenuse:.2f}")
except ValueError:
print("Please enter valid numeric values.")
RESULT:
Q: Write a program to Input a list of numbers and print the
product of it.
Soln:
def calculate_product(numbers):
# Initialize the product to 1
product = 1
for num in numbers:
product *= num
return product
# Input from the user
try:
# Take a list of numbers as input (comma-separated)
input_numbers = input("Enter a list of numbers (comma-
separated): ")
# Convert the input string to a list of numbers
number_list = [float(num.strip()) for num in
input_numbers.split(",")]
# Calculate the product
result = calculate_product(number_list)
print("The product of the entered numbers is:", result)
except ValueError:
print("Please enter valid numbers.")
RESULT:
Q: Write a program to Input a list of numbers and print the
alternate elements.
Soln:
def print_alternate_elements(numbers):
# Print elements at even indices (0, 2, 4, ...)
alternate_elements = numbers[::2]
return alternate_elements
# Input from the user
try:
# Take a list of numbers as input (comma-separated)
input_numbers = input("Enter a list of numbers (comma-separated): ")
# Convert the input string to a list of numbers
number_list = [float(num.strip()) for num in input_numbers.split(",")]
# Get alternate elements
result = print_alternate_elements(number_list)
print("The alternate elements are:", result)
except ValueError:
print("Please enter valid numbers.")
RESULT:
Q: Write a program to Input N numbers and add it in a list. Then
find the average of elements.
Soln:
def calculate_average(numbers):
# Calculate the average of the list
if len(numbers) == 0:
return 0 # Avoid division by zero
return sum(numbers) / len(numbers)
# Input from the user
try:
N = int(input("Enter the number of elements you want to input: "))
number_list = []
for i in range(N):
num = float(input(f"Enter number {i + 1}: "))
number_list.append(num)
# Calculate the average
average = calculate_average(number_list)
print(f"The average of the entered numbers is: {average:.2f}")
except ValueError:
print("Please enter valid numeric values.")
RESULT: