PAT1 Lab Experiments
PAT1 Lab Experiments
Aim
To write a Python program swap two numbers without using third variable.
Algorithm
Step1: Read a and b
Step2: Assign a=a+b
Step3: Assign b=a-b
Step4: Assign a=a-b
Step5: Print after swapping a and b values.
Python Program:
a = int(input('Enter a: '))
b = int(input('Enter b: '))
print('Before swapping a and b: ',a,b)
a=a+b
b=a-b
a=a-b
print('After swapping a and b: ',a,b)
Output:
Enter a: 55
Enter b: 22
Before swapping a and b: 55 22
After swapping a and b: 22 55
Result
Thus the Python program was executed and the results are verified.
Write a Python program to swap two numbers with using third variable.
Aim
To write a Python program swap two numbers without using third variable.
Algorithm
Step1: Read a and b
Step2: Assign temp = a
Step3: Assign a = b
Step4: Assign b = temp
Step5: Print after swapping a and b values.
Python Program:
a = int(input('Enter a: '))
b = int(input('Enter b: '))
print('Before swapping a and b: ',a,b)
temp = a
a=b
b = temp
print('After swapping a and b: ',a,b)
Output:
Enter a: 77
Enter b: 22
Before swapping a and b: 77 22
After swapping a and b: 22 77
Result
Thus the Python program was executed and the results are verified.
Write a Python program for temperature conversion convert Fahrenheit to Celsius: C = (F-32)
(5/9) and Celsius to Fahrenheit: F = C(9/5) + 32
Aim
To write a Python program for temperature conversion convert Fahrenheit to Celsius:
C = (F-32) (5/9) and Celsius to Fahrenheit: F = C(9/5) + 32
Algorithm
Step1: Read Fahrenheit
Step2: Calculate Celsius using the formula C = (F-32) (5/9)
Step3: Print Celsius
Step4: Read Celsius
Step5: Calculate Fahrenheit: F = C(9/5) + 32
Step6: Print Fahrenheit
Python Program
F = int(input('Enter Fahrenheit: '))
C = (F-32)*(5/9)
print('Celsius: %.2f'%C)
C = int(input('Enter Celsius: '))
F = C*(9/5) + 32
print('Fahrenheit: %.2f'%F)
Output
Enter Fahrenheit: 56
Celsius: 13.33
Enter Celsius: 45
Fahrenheit: 113.00
Result
Thus the Python program was executed and the results are verified.
Write a Python program to calculate Simple interest formula is given by: Simple Interest = (P
x T x R)/100 Where, P is the principal amount T is the time and R is the rate of interest
Aim
To write a Python program to calculate Simple interest formula is given by: Simple
Interest = (P x T x R)/100 Where, P is the principal amount T is the time and R is the rate of
interest
Algorithm
Step1: Read P, T, R
Step2: Calculate Simple Interest SI=(PxTxR)/100
Step3: print Simple Interest
Python Program
P = int(input('Enter the principal amount: '))
T = int(input('Enter the time: '))
R = int(input('Enter rate of interest: '))
SI = (P*T*R)/100
print('Simple Interest: %.2f'%SI)
Output
Enter the principal amount: 50000
Enter the time: 3
Enter rate of interest: 12
Simple Interest: 18000.00
Result
Thus the Python program was executed and the results are verified.
Write a Python Program to find Area and Circumference of Circle using the formula:
area=pi*r*r and circumference=2*pi*r (pi=3.14)
Aim
To write a Python program calculate area and circumference of Circle using the
formula: area=pi*r*r and circumference=2*pi*r (pi=3.14)
Algorithm
Step1: Read radius
Step2: Assign pi=3.14
Step3: Calculate area=pi*r*r
Step4: Calculate circumference=2*pi*r
Step5: print area and circumference
Python Program
r = int(input('Enter the radius: '))
pi = 3.14
area = pi*r*r
circumference = 2*pi*r
print('Area of the circle: %.2f'%area)
print('Circumference of the circle: %.2f'%circumference)
Output
Enter the radius: 5
Area of the circle: 78.50
Circumference of the circle: 31.40
Result
Thus the Python program was executed and the results are verified.
Write a Python program to check whether a number is Prime or not
Aim
To write a Python program check whether a number is Prime or not
Algorithm
Step1: Read num
Step2: if (num<1) print “num is not a Prime number” and goto end
Step3: for i = 2 to num//2+1
Step4: if (num%i)==0 print “num is not a Prime number” and goto end
Step5: next i
Step6: print “num is a prime number”
Step7: End
Python Program
num = int(input('Enter the num: '))
if num > 1:
for i in range(2, (num//2)+1):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Output
Enter the num: 13
13 is a prime number
Result
Thus the Python program was executed and the results are verified.
Write a Python program to check the given string is Palindrome or not.
Aim
To write a Python program check the given string is Palindrome or not
Algorithm
Step1: Read string s
Step2: Assign n=len(s) and f=0
Step3: for i=0 to n//2
Step4: if (s[i]!=s[n-1-i]) then f=1
Step5: next i
Step6: if f==0 then print “Palindrome” else print “Not Palindrome”
Python program
s=input('Enter the string: ')
n=len(s)
f=0
for i in range(n//2):
if s[i]!=s[n-1-i]:
f=1
break
if f==0:
print('The given string is Polindrome')
else:
print('The given string is not Polindrome')
Output
Enter the string: hello
The given string is not Polindrome
Result
Thus the Python program was executed and the results are verified.
Write a Python program to check the given number is Palindrome or not.
Aim
To write a Python program check the given number is Palindrome or not.
Algorithm
Step1: Read num
Step2: Assign rev=0 and num1=num
Step3: if num>0 go to next step else go to step5
Step4: calculate rev=rev*10+num%10 and num=num//10 then go to step3
Step5: if num1==rev then print “Palindrome” else print “Not Palindrome”
Python program
num=int(input('Enter the number: '))
rev=0
num1=num
while num>0:
rev=rev*10+num%10
num=num//10
if num1==rev:
print('The given number is Palindrome')
else:
print('The given number is not Palindrome')
Output
Enter the number: 12321
The given number is Palindrome
Result
Thus the Python program was executed and the results are verified.
Write a Python program to print the positive difference of two numbers.
Aim
To write a Python program print the positive difference of two numbers
Algorithm
Step1: Read num1 and num2
Step2: if (num1>=num2) then diff = num1 –num2
Step3: if (num2>=num1) then diff = num2 – num1
Step4: print diff
Python program
num1=int(input('Enter num1: '))
num2=int(input('Enter num2: '))
if num1>num2:
diff=num1-num2
else:
diff=num2-num1
print('The positive difference of num1 and num2 is: ',diff)
Output
Enter num1: 55
Enter num2: 88
The positive difference of num1 and num2 is: 33
Result
Thus the Python program was executed and the results are verified.
Write a Python program to check whether the given number is positive, negative, or zero.
Aim
To write a Python program check whether the given number is positive, negative or
zero
Algorithm
Step1: Read num
Step2: if (num>0) print “The given number is Positive”
Step3: if (num<0) print “The given number is Negative”
Step4: if (num=0) print “The given number is Zero”
Python program
num=int(input('Enter the number: '))
if (num>0):
print('The given number is Positive')
elif (num<0):
print('The given number is Negative')
else:
print('The given number is Zero')
Output
Enter the number: 123
The given number is Positive
Result
Thus the Python program was executed and the results are verified.
Write a Python program to display the appropriate message as per the colour of signal at the
road crossing.
Aim
To write a Python program to display the appropriate message as per the colour of
signal at the road crossing
Algorithm
Step1: Read signal
Step2: if signal==Red or red or RED then print “STOP”
Step3: if signal ==Orange or orange or ORANGE then print “Be Slow”
Step4: if signal ==Green or green or GREEN then print “GO”
Python program
signal = input("Enter the colour: ")
if signal == 'red' or signal == 'RED' or signal == 'Red':
print('STOP')
elif signal == 'orange' or signal == 'ORANGE' or signal == 'Orange':
print('Be Slow')
elif signal == 'green' or signal == 'GREEN' or signal == 'Green':
print('Go!')
else:
print('Invalid input')
Output
Enter the colour: Red
STOP
Result
Thus the Python program was executed and the results are verified.
Write a Python program to find the factors of a whole number using while loop
Aim
To write a Python program to find the factors of a whole number using while loop
Algorithm
Step1: Read num
Step2: print 1
Step3: Assign factor=2
Step4: while factor>num/2 goto step7
Step5: if num%factor == 0 then print factor
Step6: factor =factor+1 then goto step4
Step7: print num
Python program
num = int(input('Enter the number: '))
print (1, end=' ') #1 is a factor of every number
factor = 2
while factor <= num/2 :
if num % factor == 0:
print(factor, end=' ')
factor += 1
print (num, end=' ') #every number is a factor of itself
Output
Enter the number: 21
1 3 7 21
Result
Thus the Python program was executed and the results are verified.
Write a Python program to print the pattern for a number input by the user.
1
12
123
1234
12345
Aim
To write a Python program to print the pattern for a number input by the user
Algorithm
Step1: Read num
Step2: for i=1 to num
Step3: for j=1 to i
Step4: print j
Step5: next j
Step6: next i
Python program
num=int(input('Enter the number: '))
for i in range(1,num+1):
for j in range(1,i+1):
print(j,end=' ')
print()
Output
Enter the number: 5
1
12
123
1234
12345
Result
Thus the Python program was executed and the results are verified.
Write a Python program to print the pattern for a number input by the user.
1
22
333
4444
55555
Aim
To write a Python program to print the pattern for a number input by the user
Algorithm
Step1: Read num
Step2: for i=1 to num
Step3: for j=1 to i
Step4: print i
Step5: next j
Step6: next i
Python program
num=int(input('Enter the number: '))
for i in range(1,num+1):
for j in range(1,i+1):
print(i,end=' ')
print()
Output
Enter the number: 5
1
22
333
4444
55555
Result
Thus the Python program was executed and the results are verified.
Write a Python program to print the pattern for a number input by the user.
*
**
***
****
*****
Aim
To write a Python program to print the pattern for a number input by the user
Algorithm
Step1: Read num
Step2: for i=1 to num
Step3: for j=1 to i
Step4: print i
Step5: next j
Step6: next i
Python program
python program
num=int(input('Enter the number: '))
for i in range(1,num+1):
for j in range(1,i+1):
print('*',end=' ')
print()
Output
Enter the number: 5
*
**
***
****
*****
Result
Thus the Python program was executed and the results are verified.
Write a Python program to calculate the factorial of a given number
Aim
To write a Python program calculate the factorial of a given number
Algorithm
Step1: Read num
Step2: Assign fact=1
Step3: if num<0 then print “Factorial does not exist” go to end
Step4: if num=0 then print “The factorial of 0 is 1” go to end
Step5: for i=1 to num
Step6: fact=fact*i
Step7: next i
Step8: print fact
Python program
num = int(input('Enter the number: '))
fact = 1
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):
fact = fact * i
print("factorial of ", num, " is ", fact)
Output
Enter the number: 5
factorial of 5 is 120
Result
Thus the Python program was executed and the results are verified.
Write a Python program that takes the name and age of the user as input and displays a
message whether the user is eligible to apply for a driving license or not. (The eligible age is
18 years).
Aim
To write a Python program that takes the name and age of the user as input and
displays a message whether the user is eligible to apply for a driving license or not.
Algorithm
Step1: Read name and age
Step2: if (age<18) then print “User is not eligible to apply driving license”
Step3: if (age>=18) then print “User is eligible to apply driving license”
Python program
name=input('Enter the name: ')
age=int(input('Enter the age: '))
if age<18:
print(name, 'is not eligible to apply driving license')
else:
print(name,'is eligible to apply driving license')
Output
Enter the name: Ganesh Kumar
Enter the age: 25
Ganesh Kumar is eligible to apply driving license
Result
Thus the Python program was executed and the results are verified.
Write a Python program to print the table of a given number.
Aim
To write a Python program to print the table of given number
Algorithm
Step1: Read num
Step2: for i=1 to 10
Step3: print num x I =num*i
Python program
num=int(input('Enter the number: '))
for i in range(1,11):
print(num,'x',i,'=',num*i)
Output
Enter the number: 2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Result
Thus the Python program was executed and the results are verified.
Write a Python program that prints minimum and maximum of five numbers entered by the
user.
Aim
To write a Python program that prints minimum and maximum of five numbers
entered by the user.
Algorithm
Step1: Read n
Step2: for i = 1 to n
Step3: Read num
Step4: if i==1 then assign min=max=num
Step5: if (num<min) then min=num
Step6: if (num>max) then max=num
Step7: next i
Step8: print min,max
Python program
n=int(input('Enter the n: '))
print('Enter the n numbers one by one:')
for i in range(n):
num=int(input())
if i==0:
min=max=num
elif num<min:
min=num
elif num>max:
max=num
print('Minimum value is: ',min)
print('Maximum value is: ',max)
Output
Enter the n: 5
Enter the n numbers one by one:
11
22
33
44
55
Minimum value is: 11
Maximum value is: 55
Result
Thus the Python program was executed and the results are verified.
Write a Python program to check if the year entered by the user is a leap year or not.
Aim
To write a Python program check if the year entered by the user is a leap year or not.
Agorithm
Step1: Read year
Step2: if year%4 == 0 then print “The given year is leap year”
Step3: if year%4 != 0 then print “The given year is not leap year”
Python program
year=int(input('Enter the year: '))
if year%4 == 0:
print(year, 'is the leap year')
else:
print(year, 'is not leap year')
Output
Enter the year: 1212
1212 is the leap year
Result
Thus the Python program was executed and the results are verified.