M.Sc AI Practical Assignment Solution 1
Question:
Q1. Write a Python Program to,
(a) Print Multiplication Table of a Number.
(b) Check if given number is prime or not
(c) Find factorial of the given no.
Code:
def print_multiplication_table(number):
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
def factorial(number):
result = 1
for i in range(2, number + 1):
result *= i
return result
num = int(input("Enter a number: "))
print("Multiplication Table:")
print_multiplication_table(num)
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
fact = factorial(num)
print(f"The factorial of {num} is {fact}.")
Output:
Enter a number: 3 Multiplication Table: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 3 is a prime number. The factorial of 3 is 6.