Python Triangle Number Pattern: Nested Loop
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This program prints a triangular pattern of numbers. The user enters a number, and the program prints numbers from 1 to the current line number on each line.
How it works:
- Input is taken from the user and stored in variable
a. - The outer loop (
for i in range(1, a+1)) controls the line number. - The inner loop (
for j in range(1, i+1)) prints numbers 1 to i on the same line. - A
print()statement after the inner loop creates a new line.
Example: If input is 5, the output will be:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
This code is great for understanding nested loops and print formatting in Python.