Pattern 49

Pattern 49 post thumbnail image

C

#include <stdio.h>

int main()
{
  int n = 5;
  int i,j;
  int x = 1, y = n;
  int t1,t2;
  int r1,r2;


  for(i = n; i >= 1; i--)
  {
    t1 = x;
    t2 = y;
    r1 = i;
    r2 = i + 1;

    for (j = n; j >= i; j--)
    {
      if ((i + j) % 2 == 0)
      {
        printf("%2d ", t1);
      }
      else
      {
        printf("%2d ", t2);
      }
      t1 = t1 - r1++;
      t2 = t2 - r2++;
    }
    y = y + i - 1;
    x = x + i;
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>

int main()
{
  int n = 5;
  
  int x = 1, y = n;
  int t1,t2;
  int r1,r2;


  for(int i = n; i >= 1; i--)
  {
    t1 = x; t2 = y;
    r1 = i; r2 = i + 1;

    for(int j = n; j >= i; j--)
    {
      if ((i + j) % 2 == 0)
      {
        cout<<t1<<" ";
      }
      else
      {
        cout<<t2<<" ";
      }
      t1 = t1 - r1++;
      t2 = t2 - r2++;
    }
    y = y + i - 1;
    x = x + i;
    cout<<endl;
  }
  return 0;
}

Java

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

	  int x = 1;
	  int y = n;
	  int t1;
	  int t2;
	  int r1;
	  int r2;


	  for (int i = n; i >= 1; i--)
	  {
		t1 = x;
		t2 = y;
		r1 = i;
		r2 = i + 1;

		for (int j = n; j >= i; j--)
		{
		  if ((i + j) % 2 == 0)
		  {
			System.out.printf("%2d ",t1);
		  }
		  else
		  {
			System.out.printf("%2d ",t2);
		  }
		  t1 = t1 - r1++;
		  t2 = t2 - r2++;
		}
		y = y + i - 1;
		x = x + i;
		System.out.println();
	  }
	  
	}
}

C#

using System;

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

    int x = 1;
    int y = n;
    int t1;
    int t2;
    int r1;
    int r2;


    for (int i = n; i >= 1; i--)
    {
      t1 = x;
      t2 = y;
      r1 = i;
      r2 = i + 1;

      for (int j = n; j >= i; j--)
      {
        if ((i + j) % 2 == 0)
        {
          Console.Write("{0,2:D} ", t1);
        }
        else
        {
          Console.Write("{0,2:D} ", t2);
        }
        t1 = t1 - r1++;
        t2 = t2 - r2++;
      }
      y = y + i - 1;
      x = x + i;
      Console.WriteLine();
    }

    Console.ReadKey(true);
  }
}

Python

n = 5
d = 1
e = n

for x in range(n, 0, -1):

    t1 = d
    t2 = e
    r1 = x
    r2 = x + 1

    for y in range(n, x - 1, -1):
      if (x + y) % 2 == 0:
        print("{:2d} ".format(t1), end="")
      else:
        print("{:2d} ".format(t2), end="")

        t1 = t1 - r1
        t2 = t2 - r2

        r1 = r1 + 1
        r2 = r2 + 1

    e = e + x - 1
    d = d + x
    print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Patterns