Pattern 105

Pattern 105 post thumbnail image

C

#include <stdio.h>

int main()
{
  int n = 4;
  int x = 1;
  int i,j;

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

C++

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

int main()
{
  int n = 4;
  int x = 1;
  

   for(int i = 1; i <= n; i++)
  {
    for(int j = n; j >= 1; j--)
    {
      if (i >= j)
      {
       cout<<setw(4)<<(x * x);
       x++;
      }
      else
     {
      cout<<setw(4)<<" ";
     }
    }
    cout<<endl;
  }
  return 0;
}

Java

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


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

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 4;
    int x = 1;


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

    Console.ReadKey(true);
  }
}

Python

n = 4
d = 1

for x in range(1, n + 1):
  for y in range(n, 0, -1):
    if x >= y:
       print("{:4d}".format(d * d), end="")
       d += 1
    else:
       print("    ", end="")  # 4ws
  print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Patterns