Program to perform mathematical operations on two numbers.

In this python basic program, we will perform mathematical operations on two numbers defined by the user.

Steps to solve the program
  1. Take two numbers and a mathematical operation of your choice (+,-,*,/) as input.
  2. Using if-else statements perform the corresponding operations on numbers.
  3. Print the output.
				
					num1 = float(input("Enter number 1: "))
num2 = float(input("Enter number 2: "))
operation = input("Enter operation of your choice: ")

if operation == "+":
    print(num1+num2)
elif operation == "-":
    print(num1-num2)
elif operation == "/":
    print(num1/num2)
elif operation == "*":
    print(num1*num2)
else:
    print("Invalid operation")
				
			

Output :

				
					Enter number 1: 25
Enter number 2: 5
Enter operation of your choice: /
5.0
				
			

calculate the volume of a sphere.

Python program to calculate the volume of a sphere

In this python basic program, we will calculate the volume of a sphere using the formula (4/3*pi*r^2).

Steps to solve the program
  1. Take radius as input through the user.
  2. Calculate the volume of a sphere using the formula (4/3*pi*r^2).
  3. Where pi is equal to 3.14 and r is the radius of the sphere.
  4. Print the output.
				
					r = float(input("Enter radius of the Sphere: "))
volume = 4/3*(3.14*r**2)

print("Volume of Sphere: ",volume)
				
			

Output :

				
					Enter radius of the Sphere: 5
Volume of Sphere:  104.66666666666666
				
			

find the square root of a number.

perform mathematical operations on two numbers.

Python program to find the LCM of two numbers.

In this python basic program, we will find the LCM two numbers defined by the user.

Steps to solve the program
  1. Take two numbers as input through the user.
  2. Using an if-else statement check which number is greater
  3. Use While loop.
  4. Inside the While loop check whether both numbers divide the greater number or not.
  5. If yes then break the loop otherwise, add 1 to the greater number and let the loop continue.
  6. At the end print the output.
				
					num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

if num1 > num2:
       greater = num1
else:
    greater = num2

while(True):
    if((greater % num1 == 0) and (greater % num2 == 0)):
        lcm = greater
        break
    greater += 1
    
print(f"L.C.M of {num1} and {num2}: {lcm}")
				
			

Output :

				
					Enter number 1: 150
Enter number 2: 200
L.C.M of 150 and 200: 600
				
			

Program to find HCF

find the square root of a number.

Python program to find HCF of two numbers.

In this python basic program, we will find the HCF of two numbers defined by users.

Steps to solve the program
  1. Take two numbers as input through the user.
  2. Using an if-else statement check which number is smaller.
  3. Use for loop with the range function to iterate over numbers from 1 to the smaller number.
  4. During iteration check whether the iterated number divides both input numbers, if yes then assign that number to the variable.
  5. After the loop is finished print the output.
				
					num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

if num1 > num2:
    smaller = num2
else:
    smaller = num1

for i in range(1, smaller+1):
    if((num1 % i == 0) and (num2 % i == 0)):
        hcf = i 

print(f"H.C.F of {num1} and {num2}: {hcf}")
				
			

Output :

				
					Enter number 1: 150
Enter number 2: 200
H.C.F of 150 and 200: 50
				
			

find the sum of natural numbers.

Program to find LCM

Python program to find the sum of natural numbers.

In this python basic program, we will find the sum of natural numbers defined by the user.

Steps to solve the program
  1. Take the last number up to which you want to find the sum as input through the user.
  2. Create a variable to store the sum and assign its value equal to 0.
  3. Use for loop with the range function to iterate over natural numbers defined by the user.
  4. During iteration add the respective natural number to the variable that we have created to find the sum.
  5. Print the output.
				
					num = int(input("Enter a number up to which you want to find sum: "))
total = 0

for i in range(1,num+1):
    total += i
    
print("Total: ",total)
				
			

Output :

				
					Enter a number up to which you want to find sum: 10
Total:  55
				
			

convert Decimal to Binary.

Program to find HCF

Python program to convert the Decimal to Binary number.

In this basic python program, we will convert the Decimal to Binary number.

Steps to solve the program
  1. Take a decimal number as input.
  2. Convert the decimal number to its binary number using {0:b}.format(int(num)).
  3. Print the output.
				
					num = int(input("Enter a number: "))

print(f"Binary form of {num} is: ","{0:b}".format(int(num)))
				
			

Output :

				
					Enter a number: 25
Binary form of 25 is:  11001
				
			

get the current date.

find the sum of natural numbers.

Python program to get the current date using datetime.

In this python basic program, we will get the current date and time using datetime library.

Steps to solve the program
  1. Import datetime library.
  2. Get the current date and time using datetime.datetime.now().
  3. Print the output.
				
					import datetime   
dt = datetime.datetime.now() 

print("Current Date: ",dt)
				
			

Output :

				
					Current Date:  2023-01-29 09:52:33.631419
				
			

generate a random string with a specific length.

convert Decimal to Binary.

Generate a random string with a specific length.

In this python basic program, we will generate a random string with a specific length.

Steps to solve the program
  1. Import string and random library.
  2. Take the length of the string as input through the user.
  3. To generate random string with a specified length use random.choices(string.ascii_letters, k=n), where n is the length.
  4. Print the output.
				
					import string
import random
 
n = int(input("Enter length of the string: "))
 
result = ''.join(random.choices(string.ascii_letters, k=n))
 
# print result
print("The generated random string : ",str(result))
				
			

Output :

				
					Enter length of the string: 8
The generated random string :  HWyGuDWg
				
			

generate random numbers.

get the current date.

Program to generate random numbers.

In this python basic program, we will generate random numbers using random library.

Steps to solve the program
  1. Import random library.
  2. We are going to generate 5 random numbers, so use for loop with range function to generate 5 number.
  3. Use random.random() to generate random numbers inside the loop.
  4. Print the output.
				
					import random

for i in range(5):
    print(random.random())
				
			

Output :

				
					0.9199240457263513
0.9004451014782473
0.7153068440492277
0.6578686372362937
0.9954018599145891
				
			

check for the anagram.

generate a random string with a specific length.