Pattern 360

Pattern 360 post thumbnail image

C

#include <stdio.h>

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

  for(i = n; i >= 1; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (i == n && j != 1)
      {
        printf("%2d ", n * 3 - j - 1);
      }
      else if (j == 1)
      {
        printf("%2d ", n - i + 1);
      }
      else if (j == i)
      {
        printf("%2d ", (n * 2) - n + j - 1);
      }
      else
      {
        printf("   "); // 3 space
      }
    }
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>

int main()
{
  int n = 5;
  

  for(int i = n; i >= 1; i--)
  {
    for(int j = 1; j <= i; j++)
    {
      if (i == n && j != 1)
      {
        cout<<(n * 3 - j - 1)<<" ";
      }
      else if (j == 1)
      {
        cout<<(n - i + 1)<<" ";
      }
      else if (j == i)
      {
        cout<<(n * 2) - n + j - 1<<" ";
      }
      else
      {
        cout<<"  "; // 2ws
      }
    }
    cout<<endl;
  }
  return 0;
}

Java

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


	  for (int i = n; i >= 1; i--)
	  {
		for (int j = 1; j <= i; j++)
		{
		  if (i == n && j != 1)
		  {
			System.out.printf("%2d ",(n * 3 - j - 1));
		  }
		  else if (j == 1)
		  {
			System.out.printf("%2d ",(n - i + 1));
		  }
		  else if (j == i)
		  {
			System.out.printf("%2d ",(n * 2) - n + j - 1);
		  }
		  else
		  {
			System.out.print("   "); //3ws
		  }
		}
		System.out.println();
	  }
	  
	}
}

C#

using System;

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


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

    Console.ReadKey(true);

  }
}

Python

n = 5
for x in range(n, 0, -1):
  for y in range(1, x + 1):
      if x == n and y != 1:
         print(" {:2d}".format(n * 3 - y - 1), end="")
      elif y == 1:
         print(" {:2d}".format(n - x + 1), end="")
      elif y == x:
         print(" {:2d}".format((n * 2) - n + y - 1), end="")
      else:
         print("   ", end="")  # 3ws
  print()
5 1 vote
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns