write a programme to enter a number bteween 1-9 and shows like, if entered number is 5

then
it shows like
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
and also
like this
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Dani AI

Generated

The OP asked for two triangular prints. showed correct loop-based C# solutions; below are concise Python recipes, plus small tips for alignment and input checks that matter if this thread is read years later.

def pattern_descending(n):
    for i in range(1, n + 1):
        # numbers from i down to 1, separated by a single space
        print(' '.join(str(j) for j in range(i, 0, -1)))

n = int(input("Enter 1-9: "))
if not 1 <= n <= 9:
    raise ValueError("n must be between 1 and 9")
pattern_descending(n)
def pattern_right_aligned(n):
    lines = [' '.join(str(j) for j in range(1, i + 1)) for i in range(1, n + 1)]
    width = len(lines[-1])           # width of the last (widest) line
    for line in lines:
        print(line.rjust(width))

pattern_right_aligned(n)

Notes and troubleshooting:

  • For n >= 10, numbers use multiple digits and simple rjust on the full line keeps the triangle shape but columns shift. To keep columns aligned, pad each number to max_width = len(str(n)), e.g., str(j).rjust(max_width).
  • These solutions work in Python 3. Use try/except around int(input(...)) for robust input handling.
  • Time complexity is O(n^2) (building/printing all numbers), which is fine for small n like 1..9.
  • Conceptually this matches 's loop logic but uses comprehensions and join to simplify string assembly and avoid inner manual print/Write calls.
//For first pgm this s the soln....
Console.Write("Enter no b/w 1-9:");
int n=Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
Console.Write(i);
int j = i;
while (j != 1)
{
j--;
Console.Write(j);
}
if (j == 1)
{
Console.WriteLine();
}
}
Console.ReadKey();
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//for second one...
Console.Write("Enter no b/w 1-9:");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
int j = 1;
Console.Write(j);


while (j != i)
{
j++;
Console.Write(j);
}
if (j == i)
{
Console.WriteLine();
}
}
Console.ReadKey();
//;;;;;;;;;;;;;;;;;;;;;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.