Program to print the pattern A

In this program, we will print the pattern A. To write a program to print A pattern we will divide the code into 5 different sections.

section1 :    *  *  *
section2 : *  *  *  *  *
section3:  *  *      *  *
                  *  *      *  *
section4:  *  *  *  *  *
                  *  *  *  *  *
section5:  *  *      *  *
                  *  *       *  *

 

Steps to solve the program
  1. Initiate first for loop to print 1st line.
  2. Using “for i in range(9)”.
  3. Initiate second for loop to print 2nd line.
  4. Using “for j in range(9)”.
  5. Initiate third for loop to print 3rd and 4th line.
  6. Using “for k in range(2)”.
  7. Initiate a nested for loop in an above loop using “for l in range (9)”.
  8. Initiate fourth for loop to print 5th and 6th line.
  9. Using “for m in range(2)”.
  10. Initiate a nested for loop in an above loop using “for n in range(9)”.
  11. Initiate the fifth for loop to print the 7th and 8th lines.
  12. Using “for k in range(2)”.
  13. Initiate a nested for loop in an above loop using “for l in range(9)”.
				
					"""   
    * * *
  * * * * *
  * *   * *
  * *   * *
  * * * * *
  * * * * *
  * *   * *
  * *   * *
"""
# section1
for i in range(5):
    if i == 0 or i == 4:
        print(" ", end=" ")
    else:
        print("*", end=" ")

# section2
print()
for i in range(5):
    print("*", end=" ")

# section3
print()
for _ in range(2):
    for j in range(5):
        if j == 2:
            print(" ", end=" ")
        else:
            print("*", end=" ")
    print()

# section4
for _ in range(2):
    for i in range(5):
        print("*", end=" ")
    print()
    
# section5
for _ in range(2):
    for j in range(5):
        if j == 2:
            print(" ", end=" ")
        else:
            print("*", end=" ")
    print()

				
			

Output :

				
					_______________

    * * *
  * * * * *
  * *   * *
  * *   * *
  * * * * *
  * * * * *
  * *   * *
  * *   * *

________________

				
			

print number patterns

print the pyramid structure

Program to print number patterns

In this program, we will print number patterns.

Steps to solve the program

We will divide this problem into 2 part
Part 1: First will print a triangle of numbers in increasing order.
1. set variable num1 = 1
2. Initiate a for loop “for i in range(5)”
3. Initiate a nested for loop “for i in range(i+1)”
the nested loop will execute with the initiate value of the outer loop
4. print variable num1, which we initiated in step 1, and increase
its value by 1 with each iteration of the nested loop.
5. print(), will change the line after each iteration of the outer loop.

Part 2: Second will print a triangle of numbers in decreasing order.

1. Initiate a for loop with a start value of 5, an end value of 0 decreases it by -1.
“for k in range(5, 0, -1)”
2. Initiate a nested for loop with start value 0 and end value k-1
“for l in range(k+1)”
the nested loop will execute with the initial value of the outer loop
3. print variable num1, which we initiated, and decrease its value by 1 with each iteration of the nested loop.
4. print(), will change the line after each iteration of the outer loop.

				
					# Part1 : First will print triangle of numbers in increasing order.
num1 = 1
for i in range(5):
    for j in range(i+1):
        print(num1, end=" ")
        num1 += 1
    print()

# Part2 : Second will print triangle of number is decreasing order.
for k in range(5, 0, -1):
    for l in range(k-1):
        num1 -= 1
        print(num1, end=" ")
    print()
				
			

Output :

				
					1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
15 14 13 12 
11 10 9 
8 7 
6 
				
			

print the pattern T

print the pattern 

Program to print the pattern T

In this program, we will print the pattern T.

Steps to solve the program

To Solve this we will divide the problem into 2 parts.

Part 1: Print horizontal lines.

* * * * * * * * *
* * * * * * * * *

1. Initiate a first loop with range(3)
2. Initiate a nested loop with range(9), through this it will
print a line of *
3. Once both loops are completed two horizontal lines will print.

Part 2: Print vertical lines.

     * * * 
     * * * 
     * * * 
     * * * 
     * * * 

4. Now print 2 vertical lines in the middle with another nested loop.
5. Initiate a second loop with range(5)
6. Initiate a nested loop with range(9), through this it will print
line of * in vertical line.

				
					# This for loop section will print 2 horizontal lines.
for i in range(2):
    for j in range(9):
        print("*", end="")
    print()

# This for loop section will print vertical line of T pattern.
for i in range(5):
    for j in range(9):
        if j> 2 and j <6:
            print("*", end="")
        else:
            print(" ", end="")
    print()
				
			

Output :

				
					*********
*********
   ***   
   ***   
   ***   
   ***   
   ***
				
			

print the multiplication table of any number

print number patterns

Program to insert a number into an empty list

Steps to solve the program
  1. Take input through the user.
  2. Create an empty string.
  3. Use for loop to iterate over each character from the input.
  4. Check if the character is a number or not using isnumeric().
  5. If it is a number then convert it into an integer using int() and add it to the empty list.
  6. Print the output.
				
					data = "125python"
List = []

for char in data:
    if char.isnumeric():
        List.append(int(char))
        
print(List)
				
			

Output :

				
					[1, 2, 5]
				
			

count the total numbers of odd numbers between 1-100

get input from the user if it is a string insert it into an empty list

Total numbers of odd numbers between 1-100

In this program, we will total numbers of odd numbers between 1-100 .

Steps to solve the program
  1. Create a count variable and assign its value equal to 0.
  2. Use for loop with the range function to iterate over values between 1-100.
  3. If a number is an odd number then add 1 to the count variable.
  4. Print the output.
				
					count = 0

for i in range(1,101):
    if i%2 != 0:
        count += 1
        
print("Total numbers of odd number: ",count)
				
			

Output :

				
					Total numbers of odd number:  50
				
			

count total numbers of even numbers between 1-100

get input from the user if it is a number insert it into an empty list

Total numbers of even numbers between 1-100

In this program, we will count total numbers of even numbers between 1-100.

Steps to solve the program
  1. Create a count variable and assign its value equal to 0.
  2. Use for loop with the range function to iterate over values between 1-100.
  3. If a number is an even number then add 1 to the count variable.
  4. Print the output.
				
					count = 0

for i in range(1,101):
    if i%2 == 0:
        count += 1
        
print("Total numbers of even number: ",count)
				
			

Output :

				
					Total numbers of even number:  50
				
			

print the pyramid structure

count the total numbers of odd numbers between 1-100

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