0% found this document useful (0 votes)
8 views5 pages

Python Programs for Basic Calculations

The document provides a series of Python programming examples demonstrating basic mathematical operations, including addition, square root calculation, area of a triangle, solving quadratic equations, unit conversions, and percentage calculations. Each example includes code snippets and comments explaining the functionality. The examples also show how to take user input for various calculations.

Uploaded by

dk23mer1r12
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)
8 views5 pages

Python Programs for Basic Calculations

The document provides a series of Python programming examples demonstrating basic mathematical operations, including addition, square root calculation, area of a triangle, solving quadratic equations, unit conversions, and percentage calculations. Each example includes code snippets and comments explaining the functionality. The examples also show how to take user input for various calculations.

Uploaded by

dk23mer1r12
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

Example 1: Add Two Numbers with fixed values

Program

num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print ('The sum of 1.5 and 6.3 is’, sum)

output:

Example 2: Add Two Numbers with User Input


Program

# 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 1.5 and 6.3 is’, sum)

output:

Example 3: To find the square root of given number input from


user
Program

#To find the square root of any number


#let take input from user
number=float(input('Enter of any number='))
square_root=number**0.5
print('The sqaure root of given number is:',square_root)

output:

Example 4: To find the area of triangle


Program

# Python 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)

output:

Example 6: To find roots of quadratic equation


Program
# Python Program to find the roots of quadratic equation
a = 1
b = 5
c = 6
# Find two solutions

sol1 = (-b-(b**2-4*a*c) **0.5)/(2*a)

sol2 = (-b+(b**2-4*a*c) **0.5)/(2*a)

print ('The roots of quadratic equation is:', sol1, sol2)

output:

Example 7: To convert kilometres to miles


Program

# Taking kilometres input from the user

kilometres = float (input ("Enter value in kilometres: "))

# Calculate miles

miles = kilometres * (5/8)

print ('The conversion kilometres to miles is equal to %0.2f'%miles)

output:

Example 8: To convert Celsius to Fahrenheit


Program
#Fahrenheit to celsius conversion

# Take input form user

Fahrenheit = float(input('The temperature in Fahrenheit='))

# Formula for conversion is {F=C*(9/5)+32}

celsius = (Fahrenheit-32)*5/(9)

print('The conversion of celsius to fahrenheit is %0.3f'%celsius)

output:

Example 9: To find the percentage of student marks


Program

#Percentage of student marks

# Take the input from user

input ('Please enter your Roll Number:')

sub1= float (input ('Enter the subject@1 marks:'))

sub2= float(input ('Enter the subject@2 marks:'))

sub3= float(input ('Enter the subject@3 marks:'))

sub4= float(input ('Enter the subject@4 marks:'))

sub5= float(input ('Enter the subject@5 marks:'))

sub6= float(input ('Enter the subject@6 marks:'))

# Percentage calculation formula

Total_marks = sub1+sub2+sub3+sub4+sub5+sub6

Percentage = (Total_marks)/(6)

print('The percentage of given roll number is:',Percentage)

output:
Example 10: To find the distance between two points
Program

output:

Common questions

Powered by AI

The program takes a user-input number and calculates its square root by raising the number to the power of 0.5 using the exponentiation operator **. Mathematically, taking the square root of a number is equivalent to raising it to the power of 1/2, which is 0.5 in decimal form .

The program calculates the sum of two numbers by first taking input from the user, which is inherently of string type. The inputs num1 and num2 are then converted to float using the float() function to ensure numerical addition rather than string concatenation. This conversion is necessary because adding strings would result in a concatenated string, not a numeric sum .

Converting user inputs to numerical values is essential because inputs are typically read as strings. For mathematical operations like addition or finding roots, numeric data types are required to perform arithmetic computations accurately. Conversions ensure that operations result in mathematical outcomes instead of concatenating strings .

The program calculates a student's average percentage by summing the marks of six subjects and dividing the total by 6, which represents the number of subjects. Specifying six subjects ensures a consistent comparative base, necessary for averaging the performance across the specified curriculum .

The program uses Heron's formula to calculate the area of a triangle. It first computes the semi-perimeter, s, which is half the sum of the triangle's sides. This semi-perimeter is essential as it is used in Heron's formula: Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5, where a, b, and c are the sides of the triangle. The semi-perimeter helps simplify the algebraic expression needed to solve for the area .

The program converts kilometers to miles by multiplying the kilometer input by a conversion factor of 5/8. This factor simplifies the common conversion rate (approximately 0.621371 miles per kilometer) for ease in calculations, offering a roughly equivalent representation in educational contexts .

The program converts temperature from Fahrenheit to Celsius using the formula C = (F - 32) * 5/9. This involves subtracting 32 from the Fahrenheit temperature to adjust for the freezing point difference, then multiplying the result by 5/9 to scale the result into Celsius degrees accurately .

The discriminant in a quadratic equation, given as b²-4ac, determines the nature of the roots. A positive discriminant indicates two distinct real roots, zero indicates a double root, and a negative discriminant implies complex roots. The program uses the discriminant to compute the roots by inserting it in the quadratic formula solution: (-b±√(discriminant))/(2a).

Programming basic mathematical conversions enhances learning by providing practical engagement with abstract concepts. It reinforces understanding through application, improving retention and comprehension. By coding, students simulate real-world problem-solving, fostering critical thinking and precision in operational logic .

Heron's formula illustrates the intersection of algebra and geometry by using an algebraic expression to derive a geometric area. It requires calculating the semi-perimeter and then applying it in a nested algebraic formula to resolve spatial dimensions (area) through numerical methods. This synthesis highlights the utility of algebraic principles in solving geometric problems .

You might also like