Check if a number is an Armstrong number or not

In this program, we will check whether a number is an Armstrong number or not.
Definition : An Armstrong number is a one whose sum of digits raised to be the power of each digits by count of total digit.

Armstrong Number Example 371 :  3**3 + 7**3 + 1**3 = 371

Steps to solve the program
  1. Take a number as input through the user.
  2. See the given example to understand what is an Armstrong number.
  3. Create a variable and assign its value equal to 0.
  4. While the number is greater than 0 divide the number by 10 using % and add the remainder raised to 3 to the variable that we have created.
  5. Now divide the number by 10 using //.
  6. If the value of the variable is equal to the input number then it is an Armstrong number.
  7. Print the output.
				
					num = int(input("Enter a number: "))
total = 0
temp = num

while temp > 0:
    # get last digit one by one
    digit = temp % 10
    total += digit**len(str(num))
    temp //= 10

if num == total:
    print(num,"is an Armstrong number")
else:
    print(num,"is not an Armstrong number")
				
			

Output :

				
					Enter a number: 153
153 is an Armstrong number
				
			

find all prime factors of a number

print all Armstrong numbers between 1 to n

Program to find all prime factors of a number

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

Steps to solve the program
  1. Take a number as input through the user.
  2. Use for loop with range function to check for factors of the number.
  3. If a factor is divisible by 1 and by itself then it is a prime number.
  4. Use for loop with the range function to check whether a factor is a prime number or not.
  5. Print the output.
				
					n=int(input("Enter an integer:"))
print("Factors are:")

for i in range(1,n+1):
    if n%i == 0:
        count = 0
        for j in range(1,i+1):
            if i%j == 0:
                count += 1
        if count == 2:
            print(i,end=" ")
				
			

Output :

				
					Enter an integer:22
Factors are:
2 11
				
			

find the sum of all prime numbers between 1 to n

check whether a number is an Armstrong number or not

Find the sum of prime numbers between 1 to n

In this program, we will find the sum of prime numbers between 1 to n.

Steps to solve the program
  1. Take starting and ending numbers as input through the user.
  2. Create a variable and assign its value equal to 0.
  3. If a number is divisible by 1 and by itself then it is a prime number.
  4. Use for loop with the range function to check whether a number is a prime number or not.
  5. Add only those numbers to the variable.
  6. Print the output.
				
					lower = int(input("Enter strating number: "))
upper = int(input("Enter ending number: "))
total = 0
print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
    count = 0

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

print("Total sum of prime numbers: ",total)
				
			

Output :

				
					Enter strating number: 1
Enter ending number: 10
Prime numbers between 1 and 10 are:
Total sum of prime numbers:  17
				
			

print all Prime numbers between 1 to n

find all prime factors of a number

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