patternprogram_440 softethics

Pattern 440

C

#include <stdio.h>

int main()
{
  int n = 4;
  int size = 2 * (n * (n + 1) / 2);
  int x = 1, t;
  int i,j;

  for(i = 1; i <= n; i++)
  {
    t = size - (n - i);

    for(j = 1; j <= 2 * n; j++)
    {
      if (j > 2 * (i - 1))
      {
        if (j <= (2 * n) / 2 + i - 1)
        {
          printf("%02d ", x++);
        }
        else
        {
          printf("%02d ", t++);
        }
      }
      else
      {
        printf("_  "); // '_' with 2 space
      }
    }
    printf("\n");
    size = size - (n - i + 1);
  }
  return 0;
}

C++

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

int main()
{
  int n = 4;
  int size = 2 * (n * (n + 1) / 2);
  int x = 1, t;
  

  for(int i = 1; i <= n; i++)
  {
    t = size - (n - i);
    
    for(int j = 1; j <= 2 * n; j++)
    {
      if (j > 2 * (i - 1))
      {
        if (j <= (2 * n) / 2 + i - 1)
        {
          cout<<setw(3)<<x++;
          
        }
        else
        {
          cout<<setw(3)<<t++;
        }
      }
      else
      {
        cout<<setw(3)<<"_";
      } 
    }
    cout<<endl;
    size = size - (n - i + 1);
  }
  return 0;
}

Java

class PatternProg
{
	public static void main(String args[])
	{
	  int n = 4;
	  int size = 2 * (n * (n + 1) / 2);
	  int x = 1;
	  int t;


	  for (int i = 1; i <= n; i++)
	  {
		t = size - (n - i);

		for (int j = 1; j <= 2 * n; j++)
		{
		  if (j > 2 * (i - 1))
		  {
			if (j <= (2 * n) / 2 + i - 1)
			{
			  System.out.printf("%3d", x++);

			}
			else
			{
			  System.out.printf("%3d", t++);
			}
		  }
		  else
		  {
		   System.out.printf("%3c", '_');
		  }
		}
		System.out.println();
		size = size - (n - i + 1);
	  }
	  
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 4;
    int size = 2 * (n * (n + 1) / 2);
    int x = 1;
    int t;


    for (int i = 1; i <= n; i++)
    {
      t = size - (n - i);

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

          }
          else
          {
            Console.Write("{0,3:D}", t++);
          }
        }
        else
        {
          Console.Write("{0,3}", '_');
        }
      }
      Console.WriteLine();
      size = size - (n - i + 1);
    }


    Console.ReadKey(true);
  }
}

Python

n = 4
total = 2 * (n * (n + 1) // 2)
px = 1

for x in range(1, n + 1):
  py = total - (n - x)

  for y in range(1, n * 2 + 1):
    if y > 2 * (x - 1):
       if y <= (n * 2) // 2 + x - 1:
          print("{:3d}".format(px), end="")
          px += 1
       else:
          print("{:3d}".format(py), end="")
          py += 1
    else:
       print("{:3s}".format("_"), end="")
  print()
  total = total - (n - x + 1)
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments