0% found this document useful (0 votes)
3 views

AI Lab1 Program

The document contains a series of Python programming exercises, including basic tasks such as displaying 'Hello, World!', adding two numbers, calculating the area of a triangle, swapping variables, and finding the largest of three numbers. It also lists additional tasks for further practice, such as checking if a number is positive or negative, determining odd or even numbers, and finding prime numbers. Each exercise includes sample code and comments for user input options.

Uploaded by

sanawarali920
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

AI Lab1 Program

The document contains a series of Python programming exercises, including basic tasks such as displaying 'Hello, World!', adding two numbers, calculating the area of a triangle, swapping variables, and finding the largest of three numbers. It also lists additional tasks for further practice, such as checking if a number is positive or negative, determining odd or even numbers, and finding prime numbers. Each exercise includes sample code and comments for user input options.

Uploaded by

sanawarali920
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 1 AI

Q1: A simple program that displays “Hello, World!”. It's


often used to illustrate the syntax of the language.
Solution:

print('Hello, world!')

Q2: Add Two Numbers Provided by The User

# 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 {0} and {1} is {2}'.format(num1, num2, sum))

Q3 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 %0.2f' %area)

Q5 Program to swap two variables

# To take input from the user

# x = input('Enter value of x: ')

# y = input('Enter value of y: ')

x=5

y = 10

# create a temporary variable and swap the values

temp = x

x=y

y = temp

print('The value of x after swapping: {}'.format(x))

print('The value of y after swapping: {}'.format(y))


Q5 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 between",num1,",",num2,"and",num3,"is",largest)


Tasks

1. Python Program to Check if a Number is Positive, Negative or


0

2. Python Program to Check if a Number is Odd or Even

3. Python Program to Check Leap Year

4. Python Program to Find the Largest Among Three Numbers

5. Python Program to Check Prime Number

6. Python Program to Print all Prime Numbers in an Interval

7. Python Program to Find the Factorial of a Number

8. Python Program to Display the multiplication Table

You might also like