Monday, May 29, 2023

#PYTHON(DAY33)Example Programs

                             #PYTHON(DAY33)

Example Programs

1) Python Program to Solve Quadratic Equation

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

And the output is:

Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)

2) Python Program to Swap Two Variables

# Python program to swap two variables

x = 5
y = 10

# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

# create a temporary variable and swap the values
temp = x
x = y
y = temp

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

And the output is:

The value of x after swapping: 10
The value of y after swapping: 5

3) Python Program to Generate a Random Number

# Program to generate a random number between 0 and 9

# importing the random module
import random

print(random.randint(0,9))

And the output is:

5

4) Python Program to Convert Kilometers to Miles

# Taking kilometers input from the user
kilometers = float(input("Enter value in kilometers: "))

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac
print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))

And the output is:

Enter value in kilometers: 3.5
3.50 kilometers is equal to 2.17 miles

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...