TECHNO INDIA GROUP PUBLIC SCHOOL
NAME: ARNAV KUMAR RAM
CLASS: X SECTION: B ROLL: 9
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: