Program to calculate the factorial of a number

In this program, we will calculate the factorial of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable and assign its value equal to 1.
  3. While the number is greater than 0 multiply the variable by given number and subtract 1 from the number.
  4. Print the output in the end.
				
					num=int(input("Enter a number: "))
fact = 1

while num>0:
    fact = fact*num
    num = num-1

print(f"factorial of {num} is: ",fact)
				
			

Output :

				
					Enter a number: 5
factorial of 5 is:  120
				
			

find all factors of a number

check whether a number is a Prime number or not

Find all factors of a number using Python.

In this program, we will find all factors of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Divide the number by every number before it using for loop with the range function.
  3. Print only those numbers that can divide the given number completely.
				
					num = int(input("Enter a number: "))

for i in range(1,num+1):
    if num % i == 0:
        print(i,end=" ")
				
			

Output :

				
					Enter a number: 25
1 5 25 
				
			

find the power of a number

calculate the factorial of a number

Find the power of a number.

In this program, we will find the power of a number.

Steps to solve the program
  1. Take a number and power as input through the user.
  2. Create a variable and assign its value equal to 1.
  3. Multiply the created variable by the number.
  4. Repeat the above step to find out the power of the number using for loop with the range function.
  5. Print the output.
				
					num = int(input("Enter the number of which you have to find power: "))
pw = int(input("Enter the power: "))
result = 1

for n in range(pw):
    result = result * num

print(result)
				
			

Output :

				
					Enter the number of which you have to find power: 5
Enter the power: 4
625
				
			

enter a number and print it in words

find all factors of a number

Print the numbers into words

In this program, we will print numbers into words.

Steps to solve the program
  1. Take a number as input.
  2. User for loop to iterate over a number after converting it into a string using str() and create an empty string.
  3. Add the words with respect to the number to the empty string.
  4. Use if-elif statements for the purpose.
  5. Print the new string which contains numbers in the word form.
				
					num = int(input("Enter a number: "))
str1 = ""

for i in str(num):
    if i == "1":
        str1 += "One"
    elif i == "2":
        str1 += "Two"
    elif i == "3":
        str1 += "Three"
    elif i == "4":
        str1 += "Four"
    elif i == "5":
        str1 += "Five"
    elif i == "6":
        str1 += "Six"
    elif i == "7":
        str1 += "Seven"
    elif i == "8":
        str1 += "Eight"
    elif i == "9":
        str1 += "Nine"
        
print(str1)
				
			

Output :

				
					Enter a number: 236
TwoThreeSix
				
			

find the frequency of each digit in a given integer

find the power of a number

Find the frequency of each digit in a number.

In this program, we will find the frequency of each digit in a given integer.

Steps to solve the program
  1. Take a number as input and convert it into a string using str() and store it in another variable.
  2. Create an empty dictionary.
  3. Use for loop to iterate over each digit of the number and digit as key and its occurrences as the value in the dictionary.
  4. Print the output
				
					num = 12312543
str1 = str(num)
dict1 = {}

for val in str1:
    if val in dict1: 
       dict1[val] += 1:
    else:
       dict1[val] = 1
    
    
print(dict1)
				
			

Output :

				
					{'1': 2, '2': 2, '3': 2, '5': 1, '4': 1}
				
			

check whether a number is a palindrome or not

enter a number and print it in words

Check whether a number is a palindrome or not

In this program, we will check whether a number is a palindrome or not.

Steps to solve the program
  1. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  2. Use a while loop to find the reverse of a number.
  3. While num is greater than 0 divide the number by 10 using % to find the remainder.
  4. Multiply the rev by 10 and add the remainder to it.
  5. Now divide the number by 10 using //.
  6. If the reversed number is equal to the input number then it is a palindrome.
				
					num=n=int(input("Enter a number: "))
rev = 0

while n>0:
    r = n%10
    rev = (rev*10) + r
    n = n//10
print("Reverse number: ",rev)

if num == rev:
    print("It is a palindrome")
else:
    print("It is not a palindrome")
				
			

Output :

				
					Enter a number: 121
Reverse number:  121
It is a palindrome
				
			

enter a number and print its reverse

find the frequency of each digit in a given integer

Print the reverse of a number.

In this program, we will print the reverse of a number.

Steps to solve the program
  1. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  2. Use a while loop to find the reverse of a number.
  3. While num is greater than 0 divide the number by 10 using to find the remainder.
  4. Multiply the rev by 10 and add the remainder to it.
  5. Now divide number by 10 using //.
  6. Print the final output.
				
					n = int(input("Enter a number: "))
rev = 0

while n>0:
    r = n%10
    rev = (rev*10) + r
    n = n // 10

print("Reverse number: ",rev)
				
			

Output :

				
					Enter a number: 256
Reverse number:  652
				
			

calculate the product of digits of a number

check whether a number is a palindrome or not

Calculate the product of digits of a number

In this program, we will calculate the product of digits of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable product and assign its value equal to 1.
  3. Using a while loop find the sum of digits of a number.
  4. While the number is greater than 0 divide the number by 10 using % and store the remainder in a variable.
  5. Multiply the remainder by the product variable.
  6. Now divide the number by 10 using //.
  7. Print the final output.
				
					num = int(input("Enter a number: "))
product = 1

while num > 0:
    rem = num%10
    product = product * rem
    num = num // 10

print("Product of given number is: ",product)
				
			

Output :

				
					Enter a number: 56
Product of given number is:  30
				
			

calculate the sum of digits of a number

enter a number and print its reverse

Calculate the sum of digits of a number

In this program, we will calculate the sum of digits of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable total and assign its value equal to 0.
  3. Using a while loop find the sum of digits of a number.
  4. While the number is greater than 0 divide the number by 10 using and store the remainder in a variable.
  5. Add the remainder to the total variable.
  6. Now divide the number by 10 using //.
  7. Print the final output.
				
					num = int(input("Enter a number: "))
total = 0

while num > 0:
    rem = num%10
    total = total + rem
    num = num // 10

print("Sum of given number is: ",total)
				
			

Output :

				
					Enter a number: 256
Sum of given number is:  13
				
			

find the sum of the first and last digits of a number

calculate the product of digits of a number

Sum of the first and last digits of a number

In this program, we will find the sum of the first and last digits of a number.

Steps to solve the program
  1. Take a number as input and convert it into a string using str() and store it in another variable.
  2. Create a variable total and assign its value equal to 0.
  3. Use for loop with the range function to find the first and last digit of the number logically.
  4. Add only those number to the total variable.
  5. Print the output
				
					num = 2665
str1 = str(num)
total= 0

for i in range(len(str1)):
    if i == 0:
        total += int(str1[i])
    elif i == len(str1)-1:
        total += int(str1[i])
        
print("Sum of first and last number: ",total)
				
			

Output :

				
					Sum of first and last number:  7
				
			

find the first and last digits of a number

calculate the sum of digits of a number