Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Ex.

No : 1
Date :
Area and Perimeter of a Circle.
Aim :
To calculate the area and perimeter of a circle
Algorithm :

Step 1 → Input radius


Step 2 → Set Area and Perimeter as 0
Step 3 → Area = π * radius * radius
Step 4 → Perimeter = 2 *π * radius
Step 5 → Display Area , Perimeter

Program :
radius = int(input("Enter radius value : "))
Area =0
Perimeter =0
Area = π * radius * radius
Perimeter = 2 *π * radius
print("Area is", Area)
print(“Perimeter is", Perimeter)

Output :
Enter radius value : 2
Area is 12.56
Perimeter is 12.56
Result :
The area and perimeter of a circle is calculated for the given the radius.

Ex.No : 2
Date :
FIBONACCI SERIES

Aim :
To generate the Fibonacci Series for n terms using Python

Algorithm :

Step 1 → Input N
Step 2 → Set A = 0, B = 1
Step 3 → Display A, B
Step 4 → C = A + B
Step 5 → Display C
Step 6 → Set A = B, B = C
Step 7 → Repeat steps 4 to 6 for n times

Program :
num1 = int(input("Enter n value : "))
a=0
b=1
print("Fibonacci Series ..... ")
print("0 1",end=" ")
i=2
while(i <= num1-1):
c=a+b
a=b
b=c
print(c,end=" ")
i=i+1
Output :
Enter n value : 10
Fibonacci Series .....
0 1 1 2 3 5 8 13 21 34
Result :
The Fibonacci Series was generated for the given n terms.

Ex.No : 3
Date :
GCD of TWO NUMBERS
Aim :
To compute the GCD of two numbers using While loop

Algorithm :

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 is", gcd)
Output :
Enter 1st number: 84
Enter 2nd number: 75
GCD is 3
Result :
The greatest common divisor of two numbers was calculated.
1. Generate first n prime numbers
# Program for list nprime numbers
n=int(input("Enter the 'n' value : "))
print("The List of ",n," Prime numbers")
print("0 1",end= " ")
j=2
m=2
while j<=n-1:
for i in range(2,m+1):
if(m%i==0):
break
if(i==m):
print(i,end= " ")
j=j+1
m=m+1
output
Enter the 'n' value : 15
The List of 15 Prime numbers
0 1 2 3 5 7 11 13 17 19 23 29 31 37 41
2. Find the sum of squares of n natural numbers
#Program to find the sum of squares of n natural numbers

n=int(input("Enter a positive integer : "))


sum=0
for i in range(1,n+1):
m=float(input("Enter a natural number : "))
sum=sum+(m*m)

print("The sum value is ",sum)

Output
Enter a positive integer : 6
Enter a natural number : 4
Enter a natural number : 8
Enter a natural number : 6
Enter a natural number : 7
Enter a natural number : 11
Enter a natural number : 15
The sum value is 511.0
3. Find the sum of elements in an array

#Program to find the sum of numbers using list and function


def sumf(myL):
n=len(myL)
s=0
for i in range(n):
s=s+myL[i]
return(s)

n=int(input("Enter the number of elements : "))


myList=[]
for i in range(n):
val=float(input("Enter the value "))
myList.append(val)
print(myList)
print("Sum of List is : ",sumf(myList))
Output
Enter the number of elements : 6
Enter the value 55
Enter the value 45.5
Enter the value 87.7
Enter the value 45.2
Enter the value 14
Enter the value 64
[55.0, 45.5, 87.7, 45.2, 14.0, 64.0]
Sum of List is : 311.4
4. Find the largest element in an array
#Program to find the largest element in an array
def largest(myL):
n=len(myL)
maxi=myL[0]
for i in range(1,n):
if(maxi<myL[i]):
maxi=myL[i]
return(maxi)

n=int(input("Enter the number of elements : "))


myList=[]
for i in range(n):
val=float(input("Enter the value "))
myList.append(val)
print(myList)
print("The largest element of List is : ",largest(myList))
Output
Enter the number of elements : 6
Enter the value 87
Enter the value 45
Enter the value 65
Enter the value 108
Enter the value 48
Enter the value 24
[87.0, 45.0, 65.0, 108.0, 48.0, 24.0]
The largest element of List is : 108.0
5. Check if the given string is palindrome or not
# Program to check the Palindrome
myStr=input("Enter a String to check : ")
l=len(myStr)-1
i=0
myPali=True
while(i<=l):
if(myStr[i]!=myStr[l]):
print("The string '",myStr,"' is not palindrome")
myPali=False
break
i=i+1
l=l-1
if(myPali):
print("The string '",myStr,"' is palindrome"

output
>>> Enter a String to check : racecar
The string ' racecar ' is palindrome

>>> Enter a String to check : amma


The string ' amma ' is palindrome

>>> Enter a String to check : University


The string ' University ' is not palindrome
6. Store strings in a list and display them

# Program to store strings in a list and display them


cont="Y"
myList=[]
while cont.upper()=="Y":
myStr=input("Enter a String : ")
myList.append(myStr)
cont=input("Do you want to continue (Y/N)? ")

print("\n\nList elements are...\n")


n=len(myList)
for i in range(n):
print(myList[i])

Output

Enter a String : Ragu


Do you want to continue (Y/N)? Y
Enter a String : Abdhul
Do you want to continue (Y/N)? Y
Enter a String : Chrisopher
Do you want to continue (Y/N)? N

List elements are...

Ragu
Abdhul
Chrisopher
7. a) List operations without using built-in functions

# Program for list operations

# Function which return length of list


def ListLen(mL):
count = 0
for i in mL:
count += 1
return count

# Function which return reverse of list


def ListRev(mL):
l=len(mL)
mL1=[]

for i in range(n-1,-1,-1):
mL1.append(mL[i])
return mL1

#Function which return copy of list


def ListCopy(mL):
l=len(mL)
mL1=[]
for i in mL:
mL1.append(i)
return mL1

#Function which clear all elements of list


def ListClear(mL):
while(len(mL)>0):
del mL[0]
return mL

n=int(input("How many elements in a list? "))


myList=[]
for i in range(n):
myVal=int(input("Enter list element : "))
myList.append(myVal)

print("The List elements are : ")


for i in range(n):
print(myList[i],end= " ")

print("\nThe length of the List is : ",ListLen(myList))


print("The reverse of the List is : ",ListRev(myList))
print("The copy of the List is : ",ListCopy(myList))
print("The List after clear is : ",ListClear(myList))

9. b) List operations using built-in functions


# Program for list operations using built-in functions

n=int(input("How many elements in a list? "))


myList=[]
for i in range(n):
myVal=int(input("Enter list element : "))
myList.append(myVal)

print("The List elements are : ")


for i in range(n):
print(myList[i],end= " ")

print("\nThe length of the List is : ",len(myList))


myList1=myList[::-1]
print("The reverse of the List is : ",myList1)
print("The copy of the List is : ",myList.copy())
print("The List after clear is : ",myList.clear())

Output
How many elements in a list? 3
Enter list element : 12
Enter list element : 24
Enter list element : 36
The List elements are :
12 24 36
The length of the List is : 3
The reverse of the List is : [36, 24, 12]
The copy of the List is : [12, 24, 36]
The List after clear is : []
Output

How many elements in a list? 3


Enter list element : 12
Enter list element : 87
Enter list element : 54
The List elements are :
12 87 54
The length of the List is : 3
The reverse of the List is : [54, 87, 12]
The copy of the List is : [12, 87, 54]
The List after clear is : None

You might also like