0% found this document useful (0 votes)
2 views28 pages

Maths py rec

The document contains multiple Python programs demonstrating various functionalities, including temperature conversion, student grading, area calculations, prime number generation, factorial computation, Fibonacci series generation, and more. Each program includes code snippets, input prompts, and sample outputs. The programs cover a wide range of topics, showcasing basic programming concepts and operations in Python.

Uploaded by

sakthi priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views28 pages

Maths py rec

The document contains multiple Python programs demonstrating various functionalities, including temperature conversion, student grading, area calculations, prime number generation, factorial computation, Fibonacci series generation, and more. Each program includes code snippets, input prompts, and sample outputs. The programs cover a wide range of topics, showcasing basic programming concepts and operations in Python.

Uploaded by

sakthi priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

PYTHON PROGRAM TO CONVERT THE GIVEN TEMPERATURE FROM

FAHRENHEIT TO CELSIUS AND VICE VERSA DEPENDING UPON USERS


CHOICE.

PROGRAM :

Fahrenheit_1=float(input(“temperature value in degree Fahrenheit:”))

Celsius_1=(Fahrenheit_1_32)/1.8

Print(‘the%.2f degree Fahrenheit is equal to:%.2fcelsius’%(Fahrenheit_1,Celsius_1)

Print(“____OR____”)

Fahrenheit_2=float(input(“temeperature value in degree Fahrenheit:”))

Celsius_2=(farenheit_2-32)*5/9

Print(‘the %.2f degree Fahrenheit is equal to:%.2f celsius’%(Fahrenheit_2,Celsius_2))


OUTPUT :

Temperature value in degree Fahrenheit:113

The 113.00-degree Fahrenheit is equal to:45.00celsius

PYTHON PROGRAM TO CALCULATE TOTAL MARKS,PERCENTAGE AND


GRADE OF A [Link] OBTAINED IN EACH OF THE FIVE
SUBJECTS ARE TO BE INPUT BY USER.

PROGRAM:

a = float(input(“enter your marks of economics:”))

b = float(input(“enter your marks of history:”))

c = float(input(“enter your marks of cirics:”))

S=(a+b +c+)/300*100

If s>=80:

Print(“GRADE A”)

elif s>=70 and s<80:

Print(“GRADE B”)

elif S>=60 and s<70:

Print(“GRADE C”)

elif S>=40 and s<60:

Print(“GRADE D”)

elif s<40:

Print(“FAIL”)
OUTPUT :

Marks scored in economics:45

Marks scored in history:49

Marks scored in civics:50

GRADE D

CREATE A MENU DRIVEN PYTHON PROGRAM TO FIND THE


AREA OF RECTANGLE,SQUARE,CIRCLE AND TRIANGLE BY
ACCEPTING SUITABLE INPUT PARAMETERS FROM USER.

PROGRAM:

Imoprt math
Def rectangle_area(length,width:
Return length*width
Def square_area(side):
Return side*side
Def circle_area(radius):
Return [Link]*radius*radius
Def triangle_area(base,height):
Return 0.5*base*height
Def main():
While true:
Print(“\narea calculator menu:”)
Print(“[Link]”)
Print(“[Link]”)
Print(“[Link]”)
print(“[Link]”)
Print(“[Link]”)
Choice=input(“enter your choice(1-5):”)
If choice==’1’:
Length=float(input(“enter the length of the rectangle:”))
Width=float(input(“enter the width of the rectangle:”))
Print(area of rectangle:”,rectangle_area(length,width:”))
Elif choice==’2’:
Side=float(input(“enter the side length of the square:”))
Print(area of square:”,square_area(side))
Elif choice== ’3’:
Radius=float(input(“enter the radius of the circle:”))
Print(area of circle:”,circle_area(radius))
Elif choice==’4’:
Base=float(input(“enter the length of the triangle:”))
Height=float(input(“enter the height of the triangle:”))
Print(“area of triangle:”,triangle_area(base,height))
Elif choice==’5’:
Print(“exiting the [Link]!”)
Break
Else:
Print(“invalid choice!please enter a number between 1 and 5.”)
If__name__==”__main__’:
Main()

OUTPUT:
Area calculator Menu :
1. Rectangle
2. Square
3. Circle
4. Triangle
5. Exit
Enter your choice (1-5):1
Enter the length of the rectangle:2
Enter the width of the rectangle:4
Area of rectangle:8.0
Enter your choice (1-5):2
Enter the side length of the square:6
Area of square : 36.0
Enter your choice(1-5):3
Enter the radius of the circle:5
Area of ncircle: 78.53981633974483
Enter your choice(1-5):4
Enter the base length of the triangle: 6
Enter the height of the triangle: 9
Area of triangle: 27.0
Enter your choice(1-5): 5
Exiting the program. Goodgye!

PYTHON PROGRAM SCRIPT THAT PRINTS PRIME NUMBERS IN


BETWEEN GIVEN TWO NUMBERS

PROGRAM :

lower_value=int(input("Please,Enter the Lowest Range value:"))

upper_value=int(input("Pleae,Enter the Upper Range Value:"))

print("The Prime Numbers in the range are:")

for number in range(lower_value,upper_value+1):

if number>1:
for i in range(2,number):

if(number%i)==0:

break

else:

print(number)

OUTPUT :

please,Enter the Lowest Range value:14

please,Enter the Upper Range value:97

The Prime Numbers in the range are:

17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
PYTHON PROGRAM TO FIND FACTORIAL OF THE GIVEN
NUMBER USING RECURSIVE FUNCTION.

PROGRAM :

def recur_factorial(n):

if n == 1:

return n

else:

return n*recur_factorial(n-1)

num=int(input("Enter a number:"))

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

print("The factorial of",num,"is",recur_factorial(num))

OUTPUT :

Enter a number:6

The factorial of 6 is:720


PYTHON PROGRAM SCRIPT TO GENERATE THE FIBONACCI
SERIES

PROGRAM :

nterms=int(input("How many terms?"))

n1,n2=0,1

cpount=0

if nterms<=0:

print("Please enter a positive interage")

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
OUTPUT:

How many terms? 7

Fibonacci sequence:

0
1
1
2
3
5
8
PYTHON PROGRAM TO COUNT THE NUMBER OF EVEN AND
ODD NUMBERS FROM ARRAY OF N NUMBERS

PROGRAM :

arr=[1,7,8,4,5,16,8]

n=len(arr)

countEven=0

countodd=0

for i in range(n):

if arr[i]%2==0:

countEven+=1
else:
countodd+=1

print("Even Elements count:")

print(countEven)

print("Odd Elements count:")

print(countodd)

OUTPUT :

Even Element count:

Odd Element count:

3
PYTHON PROGRAM CLASS TO REVERSE A STRING WORD BY
WORD

PROGRAM :

def rev_words (string):

words = [Link](' ')

rev= ''.join(reversed(words))

return rev

s1="python is good"

print("reverse:",rev_words(s1))

s2="hello from study tonight"

print("reverse:",rev_words(s2))
OUTPUT :

reverse: goodispython

reverse: tonightstudyfromhello
A TUPLE AND A LIST AS INPUT,WRITE A PROGRAM TO COUNT
THE OCCURRENCES OF ALL ITEMS OF THE LIST IN THE
TUPLE.

PROGRAM:

From collections import Counter


Def countOccurrence(tup,Ist):
Count=0
For item in tup:
If item in Ist:
Count+=1
Return count
Tup=(‘a’,’a’,’c’,’b’,’d’,)
Ist=[’a’,’b’]
Print(countOccurrence(tup,Ist))
OUTPUT:

Occurrences:3
PYTHON PROGRAM TO CREATE A SAVINGS ACCOUNT CLASS THAT
BEHAVESJUST LIKE A BANK ACCOUNT,BUT ALSO HAS AN INTEREST
RATE AND A METHOD THAT INCREASES THE BALANCE BY THE
APPROPRIATE AMOUNT OF INTEREST.

PROGRAM :

def __init__(self, account_number, balance=0):

self.account_number = account_number

[Link] = balance

def deposit(self, amount):

[Link] += amount

print(f"Deposited ${amount}. Current balance:

$ {[Link]}")

def withdraw(self, amount):

if amount <= [Link]:

[Link] -= amount

print(f"Withdrew ${amount}. Current balance: ${[Link]}")

else:

print("Insufficient funds.")

def get_balance(self):

return [Link]

def__init__(self, account_number, balance=0,interest_rate=0.01):

super().__init__(account_number, balance)

self.interest_rate = interest_rate

def add_interest(self):

interest = [Link] * self.interest_rate


[Link] += interest

print(f"Added interest of ${interest}. current balance: ${[Link]}")

savings_acc=SavingsAccount("SA12345",1000,0.05)

print("Initial balance:",savings_acc.get_balance())

OUTPUT:

Initial balance:1000
Deposited $[Link] Balance:$1500
Withdraw $[Link] Balance:$1300
Added interest of $65.0. Current Balance: $1365.0
Final Balance: 1365.0
PYTHON PROGRAM TO CONSTRUCT THE PATTERN USING A
NESTED LOOP.

PROGRAM :

rows=int(input("enter the number of rows:"))

k=2*rows-2

for i in range(0,rows):

for j in range(0,k):

print(end=" ")

k=k-1

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

print("*",end=" ")

print("")

k=rows-2

for i in range(rows,-1,-1):

for j in range(k,0,-1):

print(end=" ")

k=k+1

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

print("*",end=" ")

print("")
OUTPUT :

Enter the number of rows :8

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
**
*
PYTHON PROGRAM TO CARRY OUT MATRIX MULTIPLICATION.

PROGRAM :

matrix1=[[1,2,3],

[4,5,6],

[7,8,9]]

matrix2=[[10,11,12],

[13,14,15],

[16,17,18]]

result=[[0,0,0],

[0,0,0],

[0,0,0]]

for i in range(len(matrix1)):

for j in range(len(matrix2[0])):

for k in range(len(matrix2)):

result[i][j]+=matrix1[i][k]*matrix2[k][i]

print("Result matrix:")

for row in result:

print(row)
OUTPUT :

Result matrix:

[84, 90, 96]

[210, 216, 231]

[318, 342, 366]


PYTHON PROGRAM SCRIPT TO GENERATE THE PASCAL
TRIANGLE.

PROGRAM :

n=5
for i in range(1, n+1):
for j in range(0, n-i+1):
print(' ', end='')

# first element is always 1


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

# first value in a line is always 1


print(' ', C, sep='', end='')

# using Binomial Coefficient


C = C * (i - j) // j
print()
OUTPUT:

1
1 1
1 2 1
1 3 3 1
1 4 6 41
PYTHON PROGRAM TO READ A FILE CONTENT AND COPY
ONLINE THE CONTENTS AT ODD LINES INTO A NEW FILE.

PROGRAM:

file1 = open(‘[Link]’, ’r’)


file2 = open(‘[Link]’, ‘w’)
lines = [Link]()
type(lines)
for I in range(0, len(lines)):
if (i%2!= 0):
[Link](lines[i])
[Link]()
[Link]()
file1 = open(‘[Link]’, ’r’)
file2 = open(‘[Link]’, ’r’)
str = [Link]()
str2 = [Link]()
print(“file1 content…”)
print(str1)
print()
print(str2)
[Link]
[Link]
OUTPUT:

file1 content…
This is line 1 of the file
This is line 2 of the file
This is line 3 of the file
This is line 4 of the file
This is line 5 of the file

file2 content…
This is line 2 of the file
This is line 4 of the file
CREATE A TURTLE GRAPHICS WINDOW WITH SPECIFIC SIZE.

PROGRAM:

Import package
Import turtle
[Link](2)
[Link](10)
For i in range (10):
[Link](40)
[Link](36)
[Link](canvwidth=400,canvheight=300,bg=”blue”)

OUTPUT:

You might also like