Pattern 123

Pattern 123 post thumbnail image

C

#include <stdio.h>

int main()
{
  int n = 5;
  int i,j,s;
  int x = 1;

  for(i = 0; i < n; i++)
  {
    for(s = 0; s<i; s++)
    {
      printf("   "); // 3ws
    }
    for (j = n - 1; j >= i; j--)
    {

      printf("%2d ", x - i);
      x++;

    }
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>
#include <iomanip.h>

int main()
{
  int n = 5;
  
  int x = 1;

  for(int i = 0; i < n; i++)
  {
    for(int s = 0; s<i; s++)
    {
      cout<<setw(3)<<" ";
    }
    for(int j = n - 1; j >= i; j--)
    {
      cout<<setw(3)<<(x - i);
      x++;
     
    }
    cout<<endl;
  }
  return 0;
}

Java

class PatternProg
{
	public static void main(String args[])
	{
	  int n = 5;

	  int x = 1;

	  for (int i = 0; i < n; i++)
	  {
		for (int s = 0; s < i; s++)
		{
		  System.out.print("   "); //3ws
		}
		for (int j = n - 1; j >= i; j--)
		{
		  System.out.printf("%3d", (x - i));
		  x++;

		}
		System.out.println();
	  }
	  
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 5;

    int x = 1;

    for (int i = 0; i < n; i++)
    {
      for (int s = 0; s < i; s++)
      {
        Console.Write("   "); //3ws
      }
      for (int j = n - 1; j >= i; j--)
      {
        Console.Write("{0,3:D}", (x - i));
        x++;

      }
      Console.WriteLine();
    }

    Console.ReadKey(true);
  }
}

Python

n = 5
d = 1

for x in range(0, n):
  for s in range(0, x):
    print("   ", end="")  # 3ws

  for y in range(n - 1, x - 1, -1):
    print("{:3d}".format(d - x), end="")
    d += 1

  print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns