Looping Structures

[Set – 3]

1. Write a Python program that asks the user to enter the number of rows and columns for a pattern to be displayed. For example, if the user enters 3 for the number of rows and 5 for the number of columns, the output should be as follows:
12345
12345
12345
Solution

2. Write a program that asks the user to enter a positive integer n and prints a triangle pattern of numbers. For, example, if user enters 4, following pattern should be displayed.
1
12
123
1234
Solution

3. Write a program that asks the user to enter a positive integer n and prints a right-angled triangle. For, example, if user enters 4, following pattern should be displayed.
      1
    12
  123
1234
Solution

4. Write a program that asks the user to enter a positive integer n and prints a reverse pyramid pattern:
1234
  123
    12
      1
Solution

5. Write a program that prints the following diamond pattern:
    1
  123
12345
  123
    1
Solution

6. Floyd's Triangle is a right-angled triangular pattern of consecutive natural numbers. Each row in the triangle contains one more number than the previous row. Write a program that takes a positive integer input n and prints Floyd's Triangle pattern of numbers. Here is a sample output:
Enter the number of rows: 4
1
2 3
4 5 6
7 8 9 10
Solution

7. Write a program that takes an integer input n and prints a pattern using the multiplication table from 1 to n. Each cell in the pattern should contain the product of its row and column numbers. Here is a sample output:

Enter a number: 5

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Solution

8. Write a program that takes an integer input n and prints all prime numbers from 2 to n using nested loops. Here is a sample output:
Enter a number: 20
2 3 5 7 11 13 17 19
Solution

9. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Write a program to find all Armstrong number in the range of 100 and 999.
Sample Output:
Armstrong numbers between 100 and 999: 153 370 371 407
Solution

10. Write a Python program to calculate the sum of the first n terms of the exponential series:

1 + x + x^2/2! + x^3/3!+ ...

Where x is a constant and n is the number of terms input by user.
Solution