AI Practical File (Programs)
AI Practical File (Programs)
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"
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
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]
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
Soln:
def is_perfect_number(num):
# A perfect number is greater than 1
if num < 1:
return False
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
RESULT:
Q: Write the program to Input the base and height of a right-
angled triangle and find the hypotenuse.
Soln:
import math
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
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
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)
RESULT: