0% found this document useful (0 votes)
26 views14 pages

AI Practical File (Python) 2022-23

Uploaded by

yomayoma.1221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
26 views14 pages

AI Practical File (Python) 2022-23

Uploaded by

yomayoma.1221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

ARTIFICIAL

INTELLIGENCE
(PRACTICAL FILE)
SUBJECT CODE: 417

(Language: Python)

Submitted By: Aarav Sachdeva


Roll Number: 01
Session: 2022-2023

____________________________________
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

6. Write a program to find the length of a string entered by the user.

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]

8. Write a program to check whether a person is eligible to cast a vote.


(Minimum voting age: 18 years)

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")

Output: Enter the first number: 8


Enter the second number: 4
Enter the third number: 2
8.0 is biggest number
Q3. Write a program to check whether the number entered is a prime
number or not.
Code: num = int(input("Enter a number: "))
x=1
if num > 1:
for i in range (2,num):
if (num%i) == 0:
x=0
break
if x == 0:
print(num," is not a prime 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)

print("Even numbers:", even_numbers)


Output:
Enter an integer and press 'enter' to add another integer
[when done type 'done' and press enter]: 98
Enter an integer and press 'enter' to add another integer
[when done type 'done' and press enter]: 28
Enter an integer and press 'enter' to add another integer
[when done type 'done' and press enter]: done
Even numbers: [98, 28]
Q8. Write a program to check whether a person is eligible to cast a vote.
(Minimum voting age: 18 years)
Code: x=int(input("Enter your age: "))
y=str(input("Enter your name: "))
if x>=18:
print(y,"is eligible to cast a vote")
else:
print("Sorry,",y,"is not eligible to cast vote yet please wait until
you turn 18")

Output: Enter your age: 15


Enter your name: Aarav
Sorry, Aarav is not eligible to cast vote yet please wait until you turn
18
Q9. 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.
Code:
numbers = []

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)

print("Original list:", numbers)


print("Reverse list:", numbers[::-1])
Output:
Please enter an integer (or 'done' to quit): 4
Please enter an integer (or 'done' to quit): 24
Please enter an integer (or 'done' to quit): 23
Please enter an integer (or 'done' to quit): 2
Please enter an integer (or 'done' to quit): 1
Please enter an integer (or 'done' to quit): 76
Please enter an integer (or 'done' to quit): 4
Please enter an integer (or 'done' to quit): done
Original list: [4, 24, 23, 2, 1, 76, 4]
Reverse list: [4, 76, 1, 2, 23, 24, 4]

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

You might also like