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.

Python program to check for the anagram.

In this python basi program, we will check for the anagram i.e. rearrangement of the letters of a word to another word, using all the original letters once.

Steps to solve the program
  1. Take a word as input and create a variable named combination and assign its value equal to 0.
  2. We have to find the number of words that can be formed by using letters available in the given word.
  3. Create a variable and assign it a value equal to the letters available in the input word.
  4. While the value of that variable is greater than 0 add 1 to combination variable and subtract 1 from the above variable.
  5. Print the output.
				
					word = input("Enter a word: ")
combination = 0
characters = len(word)

while characters>0:
    combination += characters
    characters -= 1

print("Total combinations without repetaation: ",combination)
				
			

Output :

				
					Enter a word: python
Total combinations without repetaation:  21
				
			

check leap year.

generate random numbers.

Python program to check year is a leap year.

In this python basic program, we will check whether the given year is a leap year or not.

Steps to solve the program
  1. Take a year as input through the user.
  2. Use the following condition to check whether the given year is a leap year or not – (year%400 == 0 or year%100 != 0) and year%4 == 0.
  3. Print the respective output.
				
					year = int(input("Enter a year: "))

if (year%400 == 0 or year%100 != 0) and year%4 == 0:
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")
				
			

Output :

				
					Enter a year: 1700
1700 is not a leap year
				
			

check the prime number.

check for the anagram.

Python program to check the prime number.

In this python basic program, we will check the whether the given number is a prime number.

Steps to solve the program
  1. Take a number as input through the user and create a count variable and assign its value equal to 0.
  2. Use for loop with the range function to check whether a number is a prime number or not.
  3. If a number divides the input number then add 1 to the count variable.
  4. If a number is divisible by 1 and by itself then it is a prime number.
  5. Print the output.
				
					num = int(input("Enter a number: "))
count = 0

for i in range(1,num+1):
    if num%i == 0:
        count += 1
        
if count == 2:
    print(f"{num} is a prime number")
else:
    print(f"{num} is not a prime number")
				
			

Output :

				
					Enter a number: 53
53 is a prime number
				
			

calculate compound interest.

check leap year.

Python program to calculate compound interest.

In this python basic program, we will calculate compound interest using formula p*((1+r/100)**n).

Steps to solve the program
  1. Take the principal amount, rate of interest, and a number of years as input through the user.
  2. Calculate the compound interest using the formula p((1+r/100)**n).
  3. Print the output.
				
					p = int(input("Enter principle amount: "))
r = float(input("Enter interest rate: "))
n = int(input("Enter number of years: "))

amount = p*((1+r/100)**n)

print("Compoud interest: ",amount)
				
			

Output :

				
					Enter principle amount: 10000
Enter interest rate: 10
Enter number of years: 2
Compoud interest:  12100.000000000002
				
			

check given number is palindrome or not.

check the prime number.