patternprogram_109 softethics

Pattern 109

C

#include <stdio.h>

int main()
{
  int n = 5;
  int i,j,s;
  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(s = 1; s<i; s++)
    {
      printf("   "); // 3ws
    }

    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>
#include <iomanip.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 s = 1; s<i; s++)
    {
      cout<<setw(3)<<" ";
    }

    for(int j = n; j >= i; j--)
    {
      if ((i + j) % 2 == 0)
      {
        cout<<setw(3)<<t1;
      }
      else
      {
        cout<<setw(3)<<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 s = 1; s < i; s++)
		{
		  System.out.print("   "); //3ws
		}

		for (int j = n; j >= i; j--)
		{
		  if ((i + j) % 2 == 0)
		  {
			System.out.printf("%3d", t1);
		  }
		  else
		  {
			System.out.printf("%3d", 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 s = 1; s < i; s++)
      {
        Console.Write("   "); //3ws
      }

      for (int j = n; j >= i; j--)
      {
        if ((i + j) % 2 == 0)
        {
          Console.Write("{0,3:D}", t1);
        }
        else
        {
          Console.Write("{0,3: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 s in range(1, x):
        Systym.out.print("   ") // 3
        ws

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

        if ((x + y) % 2 == 0)

            print("{:3d}".format(t1), end="")

        else:

            print("{:3d}".format(t2), end="")

        t1 = t1 - r1 + +
        t2 = t2 - r2 + +

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