Pattern 253

Pattern 253 post thumbnail image

C

#include <stdio.h>

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

  int px = 1, py = 1, t;

  for(i = 1; i <= n; i++)
  {
    t = py;
    for(j = 1; j <= i; j++)
    {
      if (i % 2 == 1)
      {
        printf("%2d ", px);
      }
      else
      {
        printf("%2d ", t);
      }
      t--;
      px++;

      if (j != i)
        printf("* ");
    }
    py = py + i + 1;
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>

int main()
{
  int n = 5;
  

  int px = 1, py = 1, t;
  
  for(int i = 1; i <= n; i++)
  {
    t = py;
    for(int j = 1; j <= i; j++)
    {
      if (i % 2 == 1)
      {
        cout<<px<<" ";
      }
      else
      {
        cout<<t<<" ";
      }
      t--;
      px++;
      
      if (j != i)
        cout<<"* ";
    }
    py = py + i + 1;
    cout<<endl;
  }
  return 0;
}

Java

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


	  int px = 1;
	  int py = 1;
	  int t;

	  for (int i = 1; i <= n; i++)
	  {
		t = py;
		for (int j = 1; j <= i; j++)
		{
		  if (i % 2 == 1)
		  {
			System.out.printf("%2d ",px);
		  }
		  else
		  {
			System.out.printf("%2d ",t);
		  }
		  t--;
		  px++;

		  if (j != i)
		  {
			System.out.print("* ");
		  }
		}
		py = py + i + 1;
		System.out.println();
	  }
	  
	}
}

C#

using System;

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


    int px = 1;
    int py = 1;
    int t;

    for (int i = 1; i <= n; i++)
    {
      t = py;
      for (int j = 1; j <= i; j++)
      {
        if (i % 2 == 1)
        {
          Console.Write("{0,2:D} ", px);
        }
        else
        {
          Console.Write("{0,2:D} ", t);
        }
        t--;
        px++;

        if (j != i)
        {
          Console.Write("* ");
        }
      }
      py = py + i + 1;
      Console.WriteLine();
    }

    Console.ReadKey(true);
  }
}

Python

n = 5

px = 1
py = 1

for x in range(1, n + 1):
  t = py
  for y in range(1, x + 1):
      if x % 2 == 1:
         print("{:2d} ".format(px), end="")
      else:
         print("{:2d} ".format(t), end="")

      t -= 1
      px += 1

      if y != x:
         print("* ", end="")

  py = py + x + 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