AI Practical File (Python) 2022-23
AI Practical File (Python) 2022-23
INTELLIGENCE
(PRACTICAL FILE)
SUBJECT CODE: 417
(Language: Python)
____________________________________
Salwan Public School
Sector-15(II), Gurgaon-122001
TABLE OF CONTENT
S.NO. PROGRAM T. Sign
1. Write a program to check whether a number taken from the user is even or odd
number.
Example: Input: 5
Output: 5 is Odd number
2. Write a program to find the largest number among the three input number.
3. Write a program to check whether the number entered is a prime number or not.
4. Write a program to print the table of any given number by the user in the given
format.
Example: 2 * 1 = 2
2*2=4
and so on….
5. Write a program to find the sum of the natural number up to n, where the ‘n’ is
the number entered by the user.
Example: if n=10
1+2+3+4+5+6+7+8+9+10 = 44
7. Write a program to ask the user to provide the integer inputs to make a list. Store
only the even values given and print the list.
Example: If input list is [1,2,3,4,5,6,7,8,9,10]
List stored and print is [2,4,6,8,10]
9. Write a program to ask the user to provide the integer inputs to make a list. Print
the list created and also the reverse order of the list.
10. Write a program to display the square of the first five natural numbers.
Example: Input 1, 2, 3, 4, 5
Output: 1, 4, 9. 16, 25
11. Write a program to find the factorial of a given number by the user.
Example:
Input: 5
Output: 5 * 4* 3* 2* 1 = 120
12. Display the Fibonacci sequence up to nth term, where the user provides the value
of n.
Example: n=10
Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Q1. Write a program to check whether a number taken from the user is
even or odd number.
Code: num = float(input("Enter a number: "))
if num%2==0:
print(num,"is an even number")
else:
print(num,"is an odd number")
Output: Enter a number: 9
9is an odd number
Q2. Write a program to find the largest number among the three input
number.
Code: num_1 = float(input("Enter the first number: "))
num_2 = float(input("Enter the second number: "))
num_3 = float(input("Enter the third number: "))
if num_3 > num_2 and num_1:
print(num_3, "is biggest number")
elif num_2 > num_1 and num_3:
print(num_2, "is biggest number")
else:
print(num_1, "is biggest number")
elif num==1:
print("1 is a unique number")
else:
print(num,"is a prime number")
Output: Enter a number: 9
9 is not a prime number
Enter a number: 3
3 is a prime number
Enter a number: 1
1 is a unique number
Q4. Write a program to print the table of any given number by the user
in the given format.
Code: x = 1
num = int(input("Enter a number: "))
while x <= 10:
print(num," * ",x," = ",num*x)
x = x+1
Output: Enter a number: 12
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120
Q5. Write a program to find the sum of the natural number up to n, where
the ‘n’ is the number entered by the user.
Code: x=int(input("Enter the value till which you want the sum: "))
sum=(x*(x+1))//2
print(sum)
Output: Enter a number till which you want to find the sum: 5
15
Q6. Write a program to find the length of a string entered by the user.
Code: num = str(input("Enter any word: "))
print(len(num))
Output: Enter any word: meisgoodatpythonhehe
20
Q7. Write a program to ask the user to provide the integer inputs to make
a list. Store only the even values given and print the list.
Code: even_numbers = []
while True:
user_input = input("Enter an integer and press ’enter’ to add
another\n[when done type ‘done’ and press enter]: ")
if user_input == "done":
break
else:
number = int(user_input)
if number % 2 == 0:
even_numbers.append(number)
while True:
user_input = input("Please enter an integer (or 'done' to quit): ")
if user_input == "done":
break
else:
number = int(user_input)
numbers.append(number)
Q10. Write a program to display the square of the first five natural
numbers.
Code: x=[1,2,3,4,5]
for a in x[0: ] :
y= a**2
print("Square of",str(a), "=", y)
Output: Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
Square of 4 = 16
Square of 5 = 25
Q11. Write a program to find the factorial of a given number by the user.
Code:
n = int(input(“enter a number: ”))
f=1
for x in range(1,n+1):
f = f*x
print(f)
Output: enter a number: 5
1
2
6
24
120
Q12. Display the Fibonacci sequence up to nth term, where the user
provides the value of n.
Code: term_1 =0
term_2 =1
n = int(input("Enter the value of n: "))
print(term_1)
print(term_2)
for a in range(n-2):
b = term_1 + term_2
term_1 = term_2
term_2 = x
print(x)
Output: Enter the value of n: 10
0
1
1
2
3
5
8
13
21
34