Print all Prime numbers between 1 to n

In this program, we will print all Prime numbers between 1 to n.

Steps to solve the program
  1. Take starting and ending numbers as input through the user.
  2. Use for loop with the range function to iterate over each number from start to end.
  3. Use for loop inside the above for loop to print all Prime numbers from start to end.
  4. If a number is divisible by 1 and by itself then it is a prime number.
  5. Print the output.
				
					lower = int(input("Enter starting number: "))
upper = int(input("Enter ending number: "))

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper):
    count = 0
    for i in range(2, num):
        if num%i == 0:
            count += 1

    if count == 0:
        print(num, end=" ")
				
			

Output :

				
					Enter strating number: 1
Enter ending number: 100
Prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
				
			

check whether a number is a Prime number

find the sum of all prime numbers between 1 to n

Check whether a number is a Prime number or not

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

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 = 1

for i in range(2, num):
    if num%i == 0:
        count += 1

if count > 1:
    print("It is not prime number")
else:
    print("It is a prime number")
				
			

Output :

				
					Enter a number: 59
It is a prime number

Enter a number: 100
It is not prime number


				
			

calculate the factorial of a number

print all Prime numbers between 1 to n

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