Pattern 331

Pattern 331 post thumbnail image

C

#include <stdio.h>
#include <math.h>

int main()
{
  int n = 5; // prefer odd
  int i,j;
  int m = n / 2 + 1;
  for (i = 1; i <= n; i++)
  {
    for (j = 1; j <= n; j++)
    {
      if (i == m || j == m)
        printf("%d ", abs(m-i+j));

      else
      {
        printf("  ");
      }

    }
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>
#include <math.h>

int main()
{
  int n = 5; // prefer odd
  
  int m = n / 2 + 1;
  for(int i = 1; i <= n; i++)
  {
    for(int j = 1; j <= n; j++)
    {
     if (i == m || j == m)
        cout<<abs(m-i+j)<<" ";
     
      else
      {
        cout<<"  "; // 2ws        
      }
      
    }
    cout<<endl;
  }
  return 0;
}

Java

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

	  int m = n / 2 + 1;
	  for (int i = 1; i <= n; i++)
	  {
		for (int j = 1; j <= n; j++)
		{
		 if (i == m || j == m)
		 {
			System.out.print(Math.abs(m - i + j)+" ");
		 }

		  else
		  {
			System.out.print("  "); //2ws
		  }

		}
		System.out.println();
	  }
	  
	}
}

C#

using System;

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

    int m = n / 2 + 1;
    for (int i = 1; i <= n; i++)
    {
      for (int j = 1; j <= n; j++)
      {
        if (i == m || j == m)
        {
          Console.Write(Math.Abs(m - i + j) + " ");
        }

        else
        {
          Console.Write("  "); //2ws
        }

      }
      Console.WriteLine();
    }

    Console.ReadKey(true);

  }
}

Python

n = 5  # prefer odd
m = n // 2 + 1

for x in range(1, n + 1):
  for y in range(1, n + 1):
      if x == m or y == m:
         print(abs(m - x + y), end=" ")
      else:
         print("  ", end="")  # 2ws
  print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns