0% found this document useful (0 votes)
53 views3 pages

Ai Exp 01

Aim (a). Write a python program to print the multiplication table for the given number? (b). Write a python program to check whether the given number is prime or not? (c) Write a python program to find factorial of the given number?
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views3 pages

Ai Exp 01

Aim (a). Write a python program to print the multiplication table for the given number? (b). Write a python program to check whether the given number is prime or not? (c) Write a python program to find factorial of the given number?
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 01

No
Aim (a). Write a python program to print the multiplication table for the
given number?
(b). Write a python program to check whether the given number is
prime or not?
(c) Write a python program to find factorial of the given number?

Software PyCharm/Spyder/Colab/etc
Requirements
Code (a)# Python program to find the multiplication table (from 1 to 10) of a
number input by the user
# take input from the user
num = int(input("Display
multiplication table of? "))
# use for loop to iterate 10
times
for i in
range(
1,11):
print(
num,'
x',i,'='
,num*
i)

(b) # Python program to check if the input number is prime or not


#num = 407
# take input from the user
num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
rem=1
for i in range(2,num):
rem=num%i
if rem == 0:
break
if rem == 0:
print(num,"is not a prime number")
else:
print(num,"is a prime number")

# if input number is less than or equal to 1, it is not prime


else:
print(num,"is not a prime number")
(c) # Python program to find the factorial of a number provided by the
user
# using recursion
def factorial(x):
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)

Theory (a) In the above program, we have taken an input integer number from
the user. Then we have iterated for loop using the range (1, 11)
function, which means greater than or equal to 1 and less than 11. In
the first iteration, the loop will iterate and multiply by 1 to the given
number. In the second iteration, 2 is multiplied by the given number,
and so on.
In our case, we have printed the table of 10. You can provide the
different numbers to test the program.

(b) A prime number is a natural number greater than 1 that has no


positive divisors other than 1 and itself. The first few prime numbers
are {2, 3, 5, 7, 11, ….}. The idea to solve this problem is to iterate
through all the numbers starting from 2 to (N/2) using a for loop and
for every number check if it divides N. If we find any number that
divides, we return false. If we did not find any number between 2 and
N/2 which divides N then it means that N is prime and we will return
True.

(c) The factorial of a number is the product of all the integers from 1 to
that number.
For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not
defined for negative numbers, and the factorial of zero is one, 0! = 1.
Note: To test the program for a different number, change the value
of num.
Here, the number whose factorial is to be found is stored in num, and
we check if the number is negative, zero or positive
using if...elif...else statement. If the number is positive, we
use for loop and range() function to calculate the factorial.
Result
Conclusion

You might also like