Print the multiplication table of any number

In this program, we will print the multiplication table of any number.

Steps to solve the program
  1. Take the number of which you want to print the table.
  2. Create a variable and assign its value equal to 0.
  3. Use a for loop to iterate over 1-10.
  4. Multiply the given number with the loop number and store the result in the created variable.
  5. Print the result after each iteration.
				
					num = int(input("enter num="))
a = 0

for i in range(1,11):
    a = i*num
    print(i,"*",num,"=",a)
				
			

Output :

				
					enter num=10
1 * 10 = 10
2 * 10 = 20
3 * 10 = 30
4 * 10 = 40
5 * 10 = 50
6 * 10 = 60
7 * 10 = 70
8 * 10 = 80
9 * 10 = 90
10 * 10 = 100
				
			

print the Fibonacci series up to n terms

print the pattern T

Print the Fibonacci series up to n terms

In this program, we will print the Fibonacci series up to n terms.

Steps to solve the program
  1. Take a number as input up to which you want to print the Fibonacci series.
  2. Create two variables num1, and num2 and assign their values equal to 0 and 1.
  3. Create a count variable and assign its values equal to 0.
  4. While the count variable is less than the input term print num1.
  5. Add num1 and num2 and store the result in a new variable n2.
  6. Change num1 to num2 and num2 to n2 and add 1 to the count variable.
  7. Print the output.
				
					num = int(input("Enter the number= "))
count = 0
num1,num2 = 0,1
print("Sequence is: ",end=" ")

while count < num:
    print(num1,end=" ")
    n2 = num1 + num2
    num1 = num2
    num2 = n2
    count += 1
				
			

Output :

				
					Enter the number= 15
Sequence is:  0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 
				
			

print all Perfect numbers between 1 to n

print the multiplication table of any number

Print all Perfect numbers between 1 to n

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

Definition of perfect number: A positive integer number that is equal to the sum of its proper divisors. The smallest perfect number is 6, its divisors are 1, 2, and 3 and their total sum is 6.
Other perfect numbers are 28, 496, and 128.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create an empty list.
  3. Use for loop to iterate over all numbers from 1 to input user.
  4. Understand what is Perfect number from the given example and add such numbers to the list.
  5. Print the output.
				
					num = int(input("Enter a number: "))

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

Output :

				
					Enter a number: 50

6 28
				
			

check whether a number is a Perfect number or not

print the Fibonacci series up to n

Check whether a number is Perfect number or not

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

Definition of perfect number: A positive integer number that is equal to the sum of its proper divisors. The smallest perfect number is 6, its divisors are 1, 2, and 3 and their total sum is 6.
Other perfect numbers are 28, 496, 128.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable and assign its value equal to 0.
  3. Use for loop with the range function to find the factors of the given numbers.
  4. Add those factors to the created variable.
  5. If the value of the variable is equal to the number then it is a perfect number.
				
					num = int(input("Enter a number: "))
total = 0

for i in range(1,num):
    if num % i == 0:
        total += i
        
if total == num:
    print("Number is a perfect number")
else:
    print("Number is not a perfect number")
				
			

Output :

				
					Enter a number: 28
Number is a perfect number
				
			

print all Armstrong numbers between 1 to n

print all Perfect numbers between 1 to n

Print all Armstrong numbers between 1 to n

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

Steps to solve the program
  1. Take the last number up to which you want to print all Armstrong numbers.
  2. Create an empty list.
  3. Use for loop to iterate over all numbers from the 1 to given number.
  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. Add that number to the empty list.
  8. Print the output.
				
					limit = int(input("Enter the last number: "))

# Iterate over all the numbers
for i in range(1, limit + 1):
    total = 0
    temp = num = i
    while temp > 0:
        digit = temp % 10
        total += digit ** len(str(i))
        temp //= 10

    if num == total:
        print(num, end=" ")
				
			

Output :

				
					Enter the last number: 500
1 2 3 4 5 6 7 8 9 153 370 371 407
				
			

check whether a number is an Armstrong number or not

check whether a number is a Perfect number or not

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