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

Class11 Listofprograms

CLASS 11 CBSE COMPUTERS SCIENCE RECOARD
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)
24 views

Class11 Listofprograms

CLASS 11 CBSE COMPUTERS SCIENCE RECOARD
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
You are on page 1/ 44

Bharatiya Vidya Bhavan’s S.V.

Vidyalaya, Tirupati
Class : XI Python Practical Record Programs

INDEX

SNO. Name of the Experiment


1 Arithmetic operations
2 Python Expressions
3 Compute X**n
4 Simple Interest
5 Compound Interest
6 Area of Triangle
7 Temperature conversions
8 Swapping of two numbers -1
9 Swapping of two numbers -2
10 Area and Circumference of a circle
11 Biggest of two numbers
12 Vote eligibility
13 Even (or) Odd
14 Simple calculator-1
15 Simple calculator-2
16 Progress sheet (Case study)
17 Electricity Bill Generations
18 Biggest of three numbers
19 Even or odd numbers - While loop
20 Nth multiplication table
21 Factorial of a given number
22 Reverse of a given number
23 Number palindrome
24 Square of a number
25 Sum of n terms
26 Floyds triangle-1,2,3
27 Sum of the series 1,2,3
28 Am strong or perfect number
29 Prime or composite number
30 Fibonacci series
31 GCD (or) LCM of a given number
32 String functions
33 Finding Minimum and Maximum value in
a List
34 Linear search in a List
35 Tuple built-in functions
36 Statistics Module
37 Random Module
Task-1
Using Basic Arithmetic operations practice expressions using
Interactive mode of Python.
Aim:
To solve expressions using Basic arithmetic operations through
Interactive mode of Python.

Commands:
>>> 6*3 >>> a=8%3
18 >>> a
>>> 3**3 2
27 >>> a=8//3
>>> 6+2*4 >>> a
14 2
>>> 5-3-3 >>> 3**2**0
-1 3
>>> 9.0**0.5 >>> 9.0%3
3.0 0.0
>>> (5+3.1)*5 >>> -9.0%3
40.5 0.0
>>> s=5.0-(3-3.0) >>> 3<5 and 5<3
>>> s False
5.0 >>> 3<=3
>>> x=12.0/4 True
>>> x >>> 3+(8*9/3)
3.0 27.0
>>> 7--7--7 >>> 12+3*4-6/2
21 21.0
>>> 8/6 >>> 12%3**4//5+6
1.3333333333333333 8
Task-2
Write Python expressions equivalent to the following arithmetic /
algebraic expressions and evaluate the output.
1 ab 6 * sum
a. ut+ at2 b. c. 8-6+ - var
2 2 7
Aim:
To Write Python expressions equivalent to the following
arithmetic/algebraic expressions

Source Code:

1
a. ut+ at2 = (u * t) + (1/2) * a * t ** 2
2
ab
b. = (a + b ) /2
2
6 * sum
c. 8-6+ - var = 8-6 + (6* sum) / 7 – var ** 1/2
7
Task-3
Write a program in Python which accepts two integers x and
n, compute xn.
Aim:
To write a program in Python which accepts two integers and x
and n, compute xn

Source Code:
# Program to compute xn
x=int(input("Enter the value of x"))
n=int(input("Enter the value of n"))
p=x**n
print("pow(x,n)=",p)

Sample input and output:

Enter the value of x:6


Enter the value of n:3
pow(x,n)= 216

Conclusion:
The above program was executed successfully
Task-4
Write a program in Python to calculate Simple Interest
( Use formula si = PTR/100 )

Aim:
To write a program in Python to calculate Simple Interest

Source Code:
# Program to Calculate Simple Interest
principal=float(input("Enter the principal amount:"))
time=int(input("Enter the time(years):"))
rate=float(input("Enter the rate:"))
simple_interest=(principal*time*rate)/100
print("The simple interest is:",simple_interest)

Sample input and output:


Enter the principal amount:5600
Enter the time(years):2
Enter the rate:6.45
The simple interest is: 722.4

Conclusion:
The above program was executed successfully
Task-5
Write a program in Python to calculate Compound Interest
( Use the formula A=P(1+r/100)n and ci=A-P )
Aim:
To write a program in Python to calculate Compound Interest

Source Code:
# Program to calculate Compound Interest
principal=float(input("Enter the principal amount:"))
time=int(input("Enter the time(years):"))
rate=float(input("Enter the rate:"))
A = principal * (1 + rate/100) ** time
CI = A - principal
print("compound interest = ", CI)

Sample input and output:


Enter the principal amount:5000
Enter the time(years):2
Enter the rate:7
compound interest = 724.5

Conclusion:
The above program was executed successfully
Task-6
Write a program in Python to calculate Area of a Triangle for
the given three sides.

Aim:
To write a program in Python to calculate Area of a Triangle

Source Code:
# Python Program to find Area of a Triangle

a = float(input('Please Enter the First side of a Triangle: '))


b = float(input('Please Enter the Second side of a Triangle: '))
c = float(input('Please Enter the Third side of a Triangle: '))
# 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 : ",round(Area,2))

Sample input and output:


Please Enter the First side of a Triangle: 18
Please Enter the Second side of a Triangle: 13
Please Enter the Third side of a Triangle: 15
The area of the triangle is : 95.92

Conclusion:
The above program was executed successfully
Task-7
Write a program in Python to convert Fahrenheit to Celsius
and Celsius to Fahrenheit
Aim:
To Write a program in Python to convert Fahrenheit to Celsius and
Celsius to Fahrenheit

Source Code:

#Fahrenheit to Celsius
f=float(input("Enter temperature in Fahrenheit :"))
c=(f-32)*5/9
print("\nTemperature in Celsius is ",round(c,2))

#Celsius to Fahrenheit
c=float(input("\nEnter temperature in Celsius :"))
f=c*9/5 +32
print("\nTemperature in Fahrenheit is ",round(f,2))

Sample input and output:

Enter temperature in Fahrenheit :100.5


Temperature in Celsius is 38.06

Enter temperature in Celsius :40.2


Temperature in Fahrenheit is 104.36

Conclusion:
The above Python program was executed successfully
Task-8
Write a program in Python to swap two numbers using a
temporary variable (third variable)
Aim:
To Write a program in Python to swap two numbers using a
temporary variable (third variable)

Source Code:

#Swapping of two numbers by using a temporary variable


a=int(input("Enter first number :"))
b=int(input("Enter second number :"))
print(" The values of a and b before swapping ",a,b)
t=a
a=b
b=t
print(" The values of a and b after swapping ",a,b)

Sample input and output:

Enter first number :18


Enter second number :26

The values of a and b before swapping 18 26


The values of a and b after swapping 26 18

Conclusion:
The above Python program was executed successfully
Task-9
Write a program in Python to swap two numbers without
numbers using a temporary variable (third variable)
Aim:
To Write a program in Python to swap two numbers without using
a temporary variable (third variable)

Source Code:

#Swapping of two numbers without using a temporary variable


a=int(input("Enter first number :"))
b=int(input("Enter second number :"))
print(" The values of a and b before swapping ",a,b)
a=a+b
b=a-b
a=a-b
print(" The values of a and b after swapping ",a,b)

Sample input and output:


Enter first number :14
Enter second number :10

The values of a and b before swapping 14 10


The values of a and b after swapping 10 14

Conclusion:
The above Python program was executed successfully
Task-10

Write a program in Python to find the diameter, Area and


Circumference of a Circle for the given radius.

Aim:
To Write a program in Python to find the diameter, Area and
Circumference of a Circle.

Source Code:
# Python Program to find the diameter, Area and the
circumference of a Circle
PI = 3.14
radius = float(input(' Enter the radius of a circle: '))
diameter = 2 * radius
circumference = 2 * PI * radius
area = PI * radius * radius
print(" \nDiameter of the Circle = " ,diameter)
print(" Circumference of the Circle =", circumference)
print(" Area of the Circle = " ,area)

Sample input and output:

Enter the radius of a circle: 13

Diameter of the Circle = 26.0


Circumference of the Circle = 81.64
Area of the Circle = 530.66

Conclusion:
The above Python program was executed successfully
Task-11

Write a program in Python to find the biggest number among


two numbers using elif statement.

Aim:
To write a program in Python to find the biggest of two numbers
using elif statement.
Source Code:
# Python Program to find the Biggest of Two Numbers
a = float(input(" Enter the First number: "))
b = float(input(" Enter the Second number: "))
if(a > b):
print( a ,” is Greater than “, b)
elif(b > a):
print( b,” is Greater than “,a)
else:
print("Both the numbers are Equal")

Sample input and output:

Enter the First number: 34


Enter the Second number: 56
56.0 is Greater than 34.0

Enter the First number: 12


Enter the Second number: 12.0
Both the numbers are Equal

Conclusion:
The above Python program was executed successfully
Task-12
Write a program in Python to check whether a person is
eligible to vote or not
Aim:
To Write a program in Python to check whether a person is eligible
to vote or not

Source Code:
#Program to check vote eligibility
age=int(input("Enter the age:"))
if age>=18:
print(" Person is eligible to vote")
else:
print(" Person is not eligible to vote")

Sample input and output:


Enter the age:21
Person is eligible to vote

Enter the age:10


Person is not eligible to vote

Conclusion:
The above Python program was executed successfully
Task-13
Write a program in Python to check whether a number is odd
or even
Aim:
To Write a program in Python to check whether a number is odd or
even

Source Code:
#Program to check Odd or Even
num=int(input("Enter the number :"))

if num%2==0:

print(num ," is an even number")

else:

print(num ," is an odd number")

Sample input and output:

Enter the number :45


45 is an odd number

Enter the number :30


30 is an even number

Conclusion:
The above Python program was executed successfully
Task-14
Write a program in Python to design a Simple Calculator by
using numbers
Aim:
To Write a program in Python to design a Simple Calculator by
using numbers

Source Code:
#Simple Calculator
n=int(input("Enter first number : "))
m=int(input("Enter second number : "))
print(" Press 1 for Addition")
print(" Press 2 for Subtraction")
print(" Press 3 for Multiplication")
print(" Press 4 for Division")
print(" Press 5 for Floor Division")
print(" Press 6 for Remainder\n")
ch=int(input("Enter your choice"))
if ch==1:
print("Addition of two numbers is ", n+m)
elif ch==2:
print("Subtraction of two numbers is ",n-m)
elif ch==3:
print("Product of two numbers is ",n*m)
elif ch==4:
print("Division of two numbers is ",n/m)
elif ch==5:
print("Floor division is ",n//m)
elif ch==6:
print("Remainder of division is ",n%m)
else:
print("Invalid choice")

Sample input and ouput:


Enter first number : 36
Enter second number : 18
Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
Press 5 for Floor Division
Press 6 for Remainder

Enter your choice2


Subtraction of two numbers is 18
Task-15
Write a program in Python to design a Simple Calculator by
using Arithmetic Operators (Symbols).

Aim:
To Write a program in Python to design a Simple Calculator by
using Arithmetic Operators (Symbols).

Source Code:

#SIMPLE CALCULATOR
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
print("Select any one operator: +,-,*,/,%,// ")
c=input("Enter the symbol:")
if c=='+':
print(a+b)
elif c=='-':
print(a-b)
elif c=='*':
print(a*b)
elif c=='/':
print(a/b)
elif c=='%':
print(a%b)
elif c=='//':
print(a//b)
else:
print("invalid")

Sample input and output:

Enter first number:31


Enter second number:20
Select any one operator: +,-,*,/,%,//
Enter the symbol:*
620

Conclusion:
The above Python program was executed successfully
Task-16
Write a program in Python to generate Student Progress
Report.

Aim:
To Write a program in Python to generate Student Progress
Report

Source Code:

#STUDENT INFORMATION FOR REPORT CARD


name=input("Enter name:")
eng=int(input("Enter English marks:"))
comp=int(input("Enter Computer Science marks:"))
phy=int(input("Enter Physics marks:"))
chem=int(input("Enter Chemistry marks:"))
bio=int(input("Enter Biology marks:"))
total=eng+comp+phy+chem+bio
per=total/5
if per>=85:
gra="A"
elif per>=75 and per<85:
gra="B"
elif per>=50 and per<75:
gra="C"
else:
gra="D"
print("------------------------------")
print("Student Report")
print("------------------------------")
print("Name : ",name)
print("Total : ",total)
print("Percent : ",per)
print("Grade : ",gra)
print("------------------------------")
Sample input and output:

Enter name:Virat
Enter English marks:67
Enter Computer Science marks:78
Enter Physics marks:67
Enter Chemistry marks:86
Enter Biology marks:91
------------------------------
Student Report
------------------------------
Name : Virat
Total : 389
Percent : 77.8
Grade : B
------------------------------

Conclusion:
The above Python program was executed successfully
Task-17
Write a program in Python to generate Electricity bill for the
given data.

Aim:
To Write a program in Python to generate Electricity bill for the
given data

Source Code:

print("*********APSPDCL Electricity Bill Generation********")


name=input("Enter customer name: ")
mtno=int(input("Enter meter number: "))
cr=float(input("Enter current reading: "))
pr=float(input("Enter previous reading: "))
nunits=cr-pr
if nunits<50:
fc=30
price=nunits*1.50+fc
elif nunits>=50 and nunits<100:
fc=70
price=nunits*2.50+fc
elif nunits>=100 and nunits<500:
fc=150
price=nunits*3.45+fc
else:
fc=200
price=nunits*4.0+fc
print(“\n”)
print("********Electricity Bill Charges********")
print("Name of the customer : ",name)
print("Customer meter number: ",mtno)
print("Current reading details : ",cr)
print("Previous reading details: ",pr)
print("Total units consumed : ",nunits)
print("Total amount to be paid : ",price)
Sample input and output:

*********APSPDCL Electricity Bill Generation********


Enter customer name: joshua
Enter meter number: 340394
Enter current reading: 8900
Enter previous reading: 5690

********Electricity Bill Charges********


Name of the customer : joshua
Customer meter number: 340394
Current reading details : 8900.0
Previous reading details: 5690.0
Total units consumed : 3210.0
Total amount to be paid : 13040.0

Conclusion:
The above Python program was executed successfully
Task-18
Write a program in Python to find the Biggest of three
numbers using if..elif ..else statement.
Aim:
To write a program in Python to find the Biggest of three numbers
using if..elif..else statement.

Source Code:
a = float(input("Please Enter the first number: "))
b = float(input("Please Enter the second number: "))
c = float(input("Please Enter the third number: "))
if (a > b and a > c):
print(a," is Greater Than", b," and",c)
elif (b > a and b > c):
print(b," is Greater Than", a," and",c)
elif (c > a and c > b):
print(c," is Greater Than", a," and",b)
else:
print("Either any two values or all the three values are equal")

Sample input and output:

Please Enter the first number: 45


Please Enter the second number: 78
Please Enter the third number: 34
78.0 is Greater Than 45.0 and 34.0

Conclusion:
The above Python program was executed successfully
Task-19
Write a program in Python to display first ‘n’ odd and even
numbers
Aim:
To write a program in Python to display first ‘n’ odd and even
numbers
Source Code:
#program to print first n odd numbers
n=int(input("Enter total number:"))
c=0
i=1
print("First ",n,"odd numbers are")
while c<n:
print(i)
c=c+1
i=i+2

#program to print first n Even numbers


n=int(input("Enter total number:"))
c=0
i=2
print("First ",n,"Even numbers are")
while c<n:
print(i)
c=c+1
i=i+2
Sample input and output:
Enter total number:5
First 5 odd numbers are
1
3
5
7
9
Enter total number:4
First 4 Even numbers are
2
4
6
8
Task-20
Write a program in Python to print nth Multiplication Table
Aim:
To write a program in Python to print nth multiplication table.

Source Code:
n=int(input("Enter table number:"))
for i in range(1,11):
print(n, "x", i , '=' , n*i)

Sample input and output:


Enter table number:7
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Conclusion:
The above Python program was executed successfully
Task-21
Write a program in Python to find the factorial of a given
number

Aim:
To write a program in Python to find the factorial of a given
number.

Source Code:
#Program to find the factorial of a given number
n=int(input("Enter the number:"))
f=1
for i in range(1,n+1):
f=f*i
print("The factorial of given number is ",f)

Sample input and output:


Enter the number:7
The factorial of given number is 5040

Conclusion:
The above Python program was executed successfully
Task-22
Write a program in Python to reverse a given number

Aim:
To write a program in Python to reverse a given number.

Source Code:
#Program to reverse a given number
n=int(input("Enter the number:"))
rev=0
while n>0:
r=n%10
rev=rev*10+r
n=n//10
print("The reverse of the given number is ",rev)

Sample input and output:

Enter the number:485


The reverse of the given number is 584

Conclusion:
The above Python program was executed successfully
Task-23
Write a program in Python to check if a given number is
Palindrome or not.

Aim:
To write a program in Python to check if a given number is
Palindrome or not

Source Code:
#Program to check if given number is palindrome
n=int(input("Enter the number:"))
num=n
rev=0
while n>0:
r=n%10
rev=rev*10+r
n=n//10
if rev==num:
print("The given number is a palindrome")
else:
print("The given number is not a palindrome")

Sample input and output:

Enter the number:6776


The given number is a palindrome

Enter the number:8764


The given number is not a palindrome

Conclusion:
The above Python program was executed successfully
Task-25
Write a program in Python to find the square of a given
number

Aim:
To write a program in Python to find the square of a given number

Source Code:
#Program to find the Square of a number
#Method-1
num=int(input("Enter the number:"))
numsq=num**2
print("The square of the given number is",numsq)

#Method-2
numsq=pow(num,2)
print("The square of the given number is",numsq)

Sample input and output:


Enter the number:7
The square of the given number is 49

Conclusion:
The above Python program was executed successfully
Task-25
Write a program in Python to find the sum of ‘n’ terms
Aim:
To write a program in Python to find the sum of ‘n’ terms

Source Code:

#Program to find the Sum of 'n' terms


# (Formula) Sum=n/2[2a+(n-1)d]
# n is number of terms, a is first term, d is difference
n=int(input("Enter the number of terms to be added:"))
a=int(input("Enter the first term"))
d= int(input("Enter the common difference"))
sum=n/2*(2*a+(n-1)*d)
print(" The sum of the series", end=' ')
print(a,a+d,a+2*d,"...is ", int(sum))

#Program to find the Sum of first 'n' terms


#Series:- 1,2,3,4,5,6,7….n
n=int(input("Enter the last term: "))
sum=n*(n+1)/2
print(" The sum of the series is ",int(sum))

Sample input and ouput:


Enter the number of terms to be added:4
Enter the first term3
Enter the common difference5
The sum of the series 3 8 13 ...is 42

Enter the last term:8


The sum of the series is 36

Conclusion:
The above Python program was executed successfully
Task-26:
Write a program in Python to generate the following patterns
(Floyds triangle) using nested loops.

Aim:

To write a program in Python to generate the following patterns using


nested loops.

Source Code:

# program to generate pattern1

n=int(input("No of rows:"))

for i in range(1,n+1):

for j in range(i):

print('*', end=' ')

print()

Sample output:

No of rows:4

**

***

****

# program to generate pattern2

n=int(input("No of rows:"))

for i in range(n,0,-1):

for j in range(1,i+1):

print(j, end=' ')

print()
Sample output:

No of rows: 4

1234

123

12

# program to generate pattern3

n=int(input("No of rows:"))

for i in range(1, n+1):

for j in range(65,65+i):

print(chr(j), end=' ')

print()

Sample output:

No of rows:4

AB

ABC

A BCD

Conclusion:
The above Python program for generating the Floyds triangle was
executed successfully
Task-27
Write a program in Python to input the value of x and n and print
the sum of the given series.

Aim:

To write a program in python to input the value of x and n and print the
sum of the given series.

Source Code:

#Program to find the sum of the series 1+x+x2+x3+.....xn

x=int(input("Enter the value of ‘x’ term:"))

n=int(input("Enter no of terms(‘n’ value):"))

sum=1

for i in range(1,n):

sum=sum+x**i

print("The sum of the terms ",end=' ')

print(1,x,x*x,"...",x**(n-1),"is",sum)

Sample output:

Enter the value of ‘x’ term:2

Enter no of terms(‘n’ value):4

The sum of the terms 1 2 4 ... 8 is 15

#Program to find the sum of the series 1-x+x2-x3+.....xn

x=int(input("Enter the value of ‘x’ term:"))

n=int(input("Enter no of terms:"))

sum=1

j=-1
for i in range(1,n):

sum=sum+(x**i)*j

j=-j

print("The sum of the terms ",end=' ')

print(1,-x,x*x,"...",-j*x**(n-1),"is",sum)

Sample output:

Enter the value of ‘x’ term:2

Enter no of term:6

The sum of the terms 1 -2 4 ... -32 is -21

#Program to find the sum of the series x+x2/2+x3/3…..+xn/n

n=int(input("Enter no of terms: "))

x=int(input("Enter value of ‘x’ term:"))

sum=0

for i in range(1,n+1):

sum=sum+x**i/i

print("The sum of the terms is ", sum)

Sample Output:

Enter no of terms: 4

Enter value of ‘x’ term:3

The sum of the terms is 36.75

Conclusion:
The above Python program for finding the sum of the series for the
given ‘x’ and ‘n’ values was executed successfully
Task-28
Write a program in Python to determine whether a number is

(1) a perfect number (2)Armstrong number

Aim:

To write a program in python to determine whether a number is a perfect


number, an Armstrong number

Source Code:

# Armstrong number is equal to sum of cubes of each digit


# Examples:1,153, 370, 371 and 407
# Perfect number, number= sum of its proper factors
# Examples 6, 28, 496, and 8128.
num=int(input("Enter any number:"))
print("1.Check for Armstrong number")
print("2.Check for Perfect number")
ch=int(input("Enter your choice:"))
if ch==1:
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num," is an Armstrong number")
else:
print(num," is not an Armstrong number")
elif ch==2:
sum = 0
number=num
for i in range(1, number//2+1):
if(number % i == 0):
sum = sum + i
if (sum == number):
print(number, " is a Perfect number")
else:
print(number, " is not a Perfect number")
else:
print("Wrong choice")
Sample output:
Enter any number:153
1.Check for Armstrong number
2.Check for Perfect number
Enter your choice:1
153 is an Armstrong number

Enter any number:28


1.Check for Armstrong number
2.Check for Perfect number
Enter your choice:2
28 is a Perfect number

Conclusion:
The above Python program was executed successfully
Task-29
Write a program in Python to determine whether the given number
is a prime or a composite number.

Aim:
To write a program in python to check if the given number is a prime or a
composite number.

Source Code:
#Program to check if the number is a prime or composite number.
num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num//2+1):
if (num % i) == 0:
print(num, "is a COMPOSITE number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is neither a Prime nor a Composite number")
else:
print()
Sample output:
Enter any number : 23
23 is a PRIME number

Enter any number : 34


34 is a COMPOSITE number

Conclusion:
The above Python program was executed successfully
Task-30
Write a program in Python to display the ‘n’ terms of a Fibonacci
series.

Aim:
To write a program in Python to display the terms of a Fibonacci series.

Source Code:
# Program to display the Fibonacci sequence up to nth term
nterms = int(input("How many terms ? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1

Sample output:
How many terms ? 6
Fibonacci sequence:
0
1
1
2
3
5

Conclusion:
The above Python program was executed successfully
Task-31
Write a program in Python to compute the greatest common
divisor(GCD) and least common multiple (LCM) of two integers

Aim:
To write a program in Python to compute the greatest common divisor
and least common multiple of two integers

Source Code:
#Program to compute GCD of two numbers
num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
i=1
while(i <= num1 and i <= num2):
if(num1 % i == 0 and num2 % i == 0):
gcd = i
i=i+1
print("GCD of two numbers is ", gcd)

#Program to compute LCM of two numbers


if num1 > num2:
big = num1
else:
big = num2
while(True):
if((big % num1 == 0) and (big % num2 == 0)):
lcm = big
break
big += 1
print(" LCM of two numbers is ", lcm)

Sample Output:
Enter 1st number: 15
Enter 2nd number: 18
GCD of two numbers is 3
LCM of two numbers is 90

Conclusion:
The above Python program was executed successfully
Task-32:
Write a program in Python to count and display the number of
vowels, consonants, uppercase, lowercase characters in a string

Aim:
To write a program in Python to count and display the number of vowels,
consonants, uppercase, lowercase characters in a string

Source Code:
#Count the no.of vowels, consonants, uppercase, lowercase
# characters in string.
str = input("Enter the string: ")
v_count=0
c_count=0
upper_count=0
lower_count=0
vowel = "aeiouAEIOU"
for alphabet in str:
if vowel.find(alphabet)!=-1:
v_count=v_count +1
elif alphabet == chr(32): #check for space
pass
else:
c_count=c_count+1
if alphabet.isupper():
upper_count += 1
elif alphabet.islower():
lower_count += 1
print("Number of Vowels :",v_count)
print("Number of Consonants :",c_count)
print("Number of upper case letters :",upper_count)
print("Number of lower case letters :",lower_count)

Sample Output:
Enter the string: Hello World
Number of Vowels : 3
Number of Consonants : 7
Number of upper case letters : 2
Number of lower case letters : 8

Conclusion:
The above Python program was executed successfully
Task -33
Write a program in python to create a List and find the minimum
and maximum value.

Aim:
To write a program in python to create a List and find the minimum
and maximum value.

Source Code:

list1 = []
# Input number of elements to put in list
num = int(input("Enter number of elements in list: "))
print("Enter the elements")
for i in range(1, num + 1):
element= int(input())
list1.append(element)
print("The Given list is",list1)
print("Smallest element in the list is:", min(list1))
print("Largest element in the list is:", max(list1))

Sample input and output:


Enter number of elements in list: 6
Enter the elements
34
23
67
19
50
38
The Given list is [34, 23, 67, 19, 50, 38]
Smallest element in the list is: 19
Largest element in the list is: 67

Conclusion:
The above Python program was executed successfully
Task 34:
Write a program in python to search for an element in a List
using linear search

Aim:
To write a program in python to search for an element in a List using
linear search

Source Code:
mylist = []
# Input number of elements to put in list
num = int(input("Enter number of elements in list: "))
print(“Enter the elements”)
for i in range(num):
value = int(input())
mylist.append(value)
element = int(input("Enter an element to be searched: "))
for i in range(num):
if element == mylist[i]:
print("\nElement found at Index:", i)
break
else:
print("Element not found")

Sample input and output:

Enter number of elements in list: 5


Enter the elements:
67
100
23
70
29
Enter an element to be searched: 45
Element not found

Conclusion:
The above Python program was executed successfully
Task 35:
Write a program in python to create a Tuple and perform basic
built-in functions
Aim:
To write a program in python to create a Tuple and perform basic
built-in functions

Source Code:

t1=(34,56,13,78,23,90)
print("The given tuple is \n",t1)
print("1.min")
print("2.max")
print("3.Sum")
print("4.Count")
print("5.sort")
print("Any other number to exit")
while(True):
ch=int(input("Enter your choice"))
if ch==1:
print(min(t1))
elif ch==2:
print(max(t1))
elif ch==3:
print(sum(t1))
elif ch==4:
a=int(input("Enter the value to be counted"))
print("The value occurs ",t1.count(a)," times")
elif ch==5:
print(sorted(t1))
else:
break
Sample input and output:
The given tuple is
(34, 56, 13, 78, 23, 90)
1.min
2.max
3.Sum
4.Count
5.sort
Any other number to exit
Enter your choice2
90
Enter your choice3
294
Enter your choice7
Task 36:

Write a Program in Python to implement the use of built-in


functions in Statistics Module

Aim :
To write a Program in Python to implement the use of built-in
functions in Statistics Module

Source Code:

import statistics as st
list1=[]
num = int(input("Enter number of elements in list: "))
print("Enter the elements")
for i in range(1, num + 1):
element= int(input())
list1.append(element)
print("The Given list is",list1)
print("Mean of the list is ",st.mean(list1))
print("Median of the list is ",st.median(list1))
print("Mode of the list is ", st.mode(list1))

Sample input and output:

Enter number of elements in list: 6


Enter the elements
213
290
890
345
765
562
The Given list is [213, 290, 890, 345, 765, 562]
Mean of the list is 510.8333333333333
Median of the list is 453.5
Mode of the list is 213

Conclusion:
The above Python program was executed successfully
Task 37:

Write a Program in Python to implement the use of built-in


functions in Random Module

Aim :
To write a Program in Python to implement the use of built-in
functions in Random Module

Source Code:

import random as rd
print(“Specify lower and upper value to generate random number”)
low=int(input("Enter the lower range"))
up=int(input("Enter the upper range"))
print("Random Number using Randint " , rd.randint(low,up))
print("Random Number using Randrange " , rd.randrange(low,up))
print("Random Number using Random (Any number)",rd.random())
print("Random Number using Random ",low+rd.random()*(up-low))

Sample input and output:

Specify lower and upper value to generate random number


Enter the lower range32
Enter the upper range45
Random Number using Randint 33
Random Number using Randrange 37
Random Number using Random (Any number) 0.2173837485147735
Random Number using Random 37.75195488105927

Conclusion:
The above Python program was executed successfully

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy