patternprogram_198 softethics

Pattern 198

C

#include <stdio.h>

int main()
{
  int n = 5;// prefer odd
  int i,j;
  int x = 1, y = n * (n * 2) - (n - 1);
  int px,py;

  for(i = 1; i <= n; i++)
  {
    px = x;
    py = y;

    for (j = 1; j <= n; j++)
    {
      if (i <= n / 2 + 1)
      {
        printf("%2d ", px++);
      }
      else
      {
        printf("%2d ", py++);
      }
    }
    printf("\n");
    x = x + (n * 2);
    y = y - (n * 2);
  }
  return 0;
}

C++

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

int main()
{
  int n = 5;// prefer odd 
  
  int x = 1, y = n * (n * 2) - (n - 1);
  int px,py;

  for(int i = 1; i <= n; i++)
  {
    px = x; py = y;

    for(int j = 1; j <= n; j++)
    {
      if (i <= n / 2 + 1)
      {
        cout<<setw(3)<<px++;
      }
      else
      {
        cout<<setw(3)<<py++;
      }
    }
    cout<<endl;
    x = x + (n * 2);
    y = y - (n * 2);
  }
  return 0;
}

Java

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

	  int x = 1;
	  int y = n * (n * 2) - (n - 1);
	  int px;
	  int py;

	  for (int i = 1; i <= n; i++)
	  {
		px = x;
		py = y;

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

C#

using System;

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

    int x = 1;
    int y = n * (n * 2) - (n - 1);
    int px;
    int py;

    for (int i = 1; i <= n; i++)
    {
      px = x;
      py = y;

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

    Console.ReadKey(true);
  }
}

Python

n = 5  # prefer odd
d = 1
e = n * (n * 2) - (n - 1)

for x in range(1, n + 1):

  px = d
  py = e

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