AI Practical File Part 1 2024-25
Q1. This program adds two numbers
Ans.
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print(“The sum of num1 & num2:”, sum)
Output :
The sum of 1.5 and 6.3 is 7.8
Q2. Store input numbers and sum of this
Ans.
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print(“The sum of two numbers : ” sum)
Output :
Enter first number: 1.5
Enter second number: 6.3
The sum of two numbers : 7.8
Q3. Python Program to find the area of triangle
Ans.
# Python Program to find the area of triangle
a=5
b=6
c=7
# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print(“The area of the triangle is”, area)
Output :
The area of the triangle is 14.70
Q4. Python program to swap two variables
Ans.
# Python program to swap two variables
x=5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x=y
y = temp
print("The value of x after swapping",x)
print("The value of y after swapping",y)
Output :
The value of x after swapping: 10
The value of y after swapping: 5
Q5. Python Program to calculate the square root
Ans.
# Python Program to calculate the square root
# Note: change this value for a different result
num = 8
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print("The square root of is",num_sqrt)
Output :
The square root of 8.000 is 2.828
Q6. Python Program to convert temperature in celsius to Fahrenheit
Ans.
# Python Program to convert temperature in celsius to fahrenheit
# change this value for a different result
celsius = 37.5
# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print(“The temperature in Fahrenheit”,fahrenheit)
Output :
37.5 degree Celsius is equal to 99.5 degree Fahrenheit
Q7. Write a Python Program to Solve Quadratic Equation
Ans.
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module
import cmath
a=1
b=5
c=6
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Output :
The solutions are (-3+0j) and (-2+0j)
Q8. Python Program to find the largest number among the three input numbers
Ans.
# Python program to find the largest number among the three input numbers
# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output :
The largest number is 14
Q9. Python Program to find the Positive or Negative numbers or Zero
Ans.
(I)
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output (I) :
Enter a number: 2
Positive number
(II)
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output (II) :
Enter a number: 0
Zero
Q10. Python Program to check if the input number is odd or even.
Ans.
# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("The Even No. is :",num)
else:
print("The Odd No. is :",num)
Output (I) :
Enter a number: 43
43 is Odd
Output (II) :
Enter a number: 18
18 is Even
Q11. Write a Python Program to Find the Factorial of a Number
Ans.
# Python program to find the factorial of a number provided by the user.
# change the value for a different result
num = 7
# To take input from the user
#num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Output :
The factorial of 7 is 5040
Q12. Write a Python Program to Print the Fibonacci sequence
Ans.
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output :
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8