Pattern 107

Pattern 107 post thumbnail image

C

#include <stdio.h>

int main()
{
  int n = 5;
  int i,j;
  int k = (n * (n + 1)) / 2;


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

C++

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

int main()
{
  int n = 5; 
  
  int k = (n * (n + 1)) / 2;
  

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

Java

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

	  int k = (n * (n + 1)) / 2;


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

C#

using System;

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

    int k = (n * (n + 1)) / 2;


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

    Console.ReadKey(true);
  }
}

Python

n = 5
k = (n * (n + 1)) // 2

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

Related Patterns