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

Python AI Practical File Programs

Uploaded by

yomayoma.1221
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Python AI Practical File Programs

Uploaded by

yomayoma.1221
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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
[Link]. 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)
[Link](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

Common questions

Powered by AI

The program uses a formula `(n*(n+1))//2` for efficiency, avoiding loops. For readability, it's beneficial to include comments explaining the formula. Using meaningful variable names could improve code readability further, e.g., `max_number` instead of `x` and `total_sum` instead of `sum` .

The algorithm checks if a number is greater than 1 and proceeds to test divisibility starting from 2 up to the number minus one. If any divisor results in a zero remainder, the number is not prime; otherwise, it is prime. The edge case for the number 1 is handled separately as a unique number. An improvement could incorporate divisibility checks only up to the square root of the input number for efficiency .

The program collects age and name from the user, then checks the age to determine eligibility. The output is personalized with the user's name. An improvement could be input validation, ensuring age is numeric, and potentially a more user-friendly prompt for entering both age and name at once .

The loop iterates from 1 to n, cumulatively multiplying values to compute the factorial. This approach is straightforward but may lack efficiency for extremely large numbers due to recursion limits or memory constraints. An improvement could involve using an iterative approach or Python’s `math.factorial` for built-in optimizations .

The program checks if a number is even or odd by using the modulus operator: `num % 2 == 0` determines if a number is even, otherwise it is odd. An improvement is to handle float inputs correctly by converting them to integers before the modulus operation, as the program currently uses `float(input(...))` which can lead to unexpected behavior with decimal numbers. Converting to integer can be achieved by replacing `num = float(input(...))` with `num = int(float(input(...)))` .

The program uses a loop to continuously accept input, converting it to an integer and storing it in a list if even. The use of a try-except block around `int(user_input)` would improve robustness by catching and handling invalid inputs (non-integers), ensuring the program doesn’t crash when such inputs are provided, which the current design does not encompass .

The program uses a loop (`while` or `for`) with a counter variable to iterate through numbers 1 to 10, printing each result in a fixed format. Flexibility could be enhanced by allowing the user to specify the range instead of hardcoding 1 to 10, and using formatted strings (f-strings) for clearer syntax and easier adjustments of format .

The program initializes the first two terms as 0 and 1, then iterates for n-2 times. Each loop iteration calculates the next term as the sum of the two previous terms, updates the previous terms, and prints the result. A common mistake is incorrectly updating terms or using improper loop bounds, e.g., iterating n times instead of n-2 .

The logical error might occur because of incorrect compound conditionals: `if num_3 > num_2 and num_1` and `elif num_2 > num_1 and num_3` are incorrect. Corrections include using: `if num_3 > num_2 and num_3 > num_1` and `elif num_2 > num_1 and num_2 > num_3` to properly compare the values of num_1, num_2, and num_3 .

The program uses slicing `[::-1]` to reverse the list efficiently after collecting input integers into a list. One issue is the lack of validation for integer inputs; currently, it uses a try-except block, which would prevent the program from crashing if non-integer values are accidentally submitted by users .

You might also like