Subscribe Us

header ads
Powered By Blogger

PYTHON PROGRAM

Simple Hello Program

#This program prints Hello, User!

print('Hello, User!')

Program to add two given numbers.

#This program adds two numbers
num1 = 23.25
num2 = 25.26
#Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of two number = ', sum)

Program to add two numbers provided by users.

#This program adds two numbers
num1 = input('Enter the first number:-')
num2 = input('Enter the second number :-')
#Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of two number =',sum)

Program to find the square root of given number.

#This program calculate the square root.
#Note if you want to find the square root of other number then change this number.
num = 8
#Logic for calculating square root
num_sqrt = num** 0.5
# Display the square root of given number.
print('The square root of number = ', num_sqrt)

Program to find the square root of the number provided by user.

#This program calculate the square root.
#To take input from the user.
num = float(input('Enter the number:- '))
#Logic for calculating square root
num_sqrt = num** 0.5
# Display the square root of given number.
print('The square root of number = ', num_sqrt)

Program to find the square root of given complex number.

#This program calculate the square root of complex number.
#Importing the complex math module.
import cmath
num = 1 + 2j
#Logic to calculate square root of complex number.
num_sqrt = cmath.sqrt(num)
# Display the square root of given number.
print('The square root of given complex number = ', num_sqrt)

Program to find the square root of complex number provided by user.

#This program calculate the square root of complex number.
#Importing the complex math module.
import cmath
#To take input from the user
num = eval(input('Enter a number : - '))
#Logic to calculate square root of complex number.
num_sqrt = cmath.sqrt(num)
# Display the square root of given number.
print('The square root of provided complex number = ', num_sqrt)


Program to calculate the area of triangle when all the three sides of triangle are given.

#This program calculate the area of triangle when all sides are given.
a = 5
b = 6
c = 7
#Logic to calculate the area of triangle using heron's formula.
# First calculate the semi-perimeter
s = (a + b + c) / 2
# Logic to calculate the area of triangle
area = (s*(s-a)*(s-b)*(s-c))**0.5
# Display the area of the triangle.
print('The area of the triangle =  ', area)

Program to calculate the area of triangle when all the three sides of triangle are given by user.

#This program calculate the area of triangle when all sides are given.
#The length of side provided by users
a = float(input('Enter the first side a  = '))
b = float(input('Enter the first side b  = '))
c = float(input('Enter the first side c  = '))
#Logic to calculate the area of triangle using heron's formula.
# First calculate the semi-perimeter
s = (a + b + c) / 2
# Logic to calculate the area of triangle
area = (s*(s-a)*(s-b)*(s-c))**0.5
# Display the area of the triangle.
print('The area of the triangle =  ', area)

Program to swap two given variable or number using temporary variable.

#This program is to swap the given variable or number.
x = 10
y = 20
#Display the original entered number
print('The value of x before swapping  = ', x)
print('The value of y before swapping = ', y)
#Logic to swap the given variable or a number.
# First create a temporary variable and then swap the values
temp = x
x = y
y = temp
# Display the swapped number
print('The value of x after swapping  = ', x)
print('The value of y after swapping = ', y)

Program to swap two variable or number provided by user without using temporary variable.

#This program is to swap the given variable or number.
# To take input from the user
x = input('Enter the value of x = ')
y = input('Enter the value of y = ')
#Display the original entered number
print('The value of x before swapping  = ', x)
print('The value of y before swapping = ', y)
#Logic to swap the given variable or a number.
# First create a temporary variable and then swap the values
x, y = y, x
# Display the swapped number
print('The value of x after swapping  = ', x)
print('The value of y after swapping = ', y)


Program to convert kilometers into miles.

#This program is to convert the given kilometers into miles.
# To take input from the user
kilometers = float(input("Enter the value in Kilometers :- ")
#Logic to converts the kilometers into miles
# First define conversion factor
conv_fac = 0.621371
# It convert kilometer into miles
miles = kilometers * conv_fac
# Display the converted value of mile equivalent to entered value of kilometers
print('The entered value of kilometer is  =   ', kilometers)
print('The converted value of  mile equivalent to entered value of kilometers i.e. miles  = ', miles)

Program to convert degree Celsius  into degree Fahrenheit.

#This program is to convert degree Celsius into degree Fahrenheit
# To take input from the user
Celsius = float(input("Enter the value in Celsius:- ")
#Logic to converts the degree Celsius into degree Fahrenheit
Fahrenheit = (Celsius * 1.8) + 32
# Display the converted value of mile equivalent to entered value of kilometers
print('The entered value of degree Celsius =   ', Celsius)
print('The converted value of  Fahrenheit equivalent to entered value of degree Celsius i.e. Fahrenheit  = ', Fahrenheit)

Program to check whether the entered number is a positive number, a negative number or 0 using if ...elif....else

#This program is to check whether the entered number is a positive number, a negative number or 0
# To take input from the user
num = float(input("Enter a number : - ")
#Logic to converts the degree Celsius into degree Fahrenheit
if num > 0:
    print("Entered number is a Positive number")
elif num == 0:
    print("Entered number is 0")
else:
    print("Entered number is Negative number")

Program to check whether the entered number is a positive number, a negative number or 0 using nested if

#This program is to check whether the entered number is a positive number, a negative number or 0
# To take input from the user
num = float(input("Enter a number : - ")
#Logic to converts the degree Celsius into degree Fahrenheit
if num >=0:
    if num == 0:
    print("Entered number is ZERO")
else :
    print("Entered number is Positive number")
else:
    print("Entered number is Negative number")


0 Comments:

Post a Comment