patternprogram_248 softethics

Pattern 248

C

#include <stdio.h>

int main()
{
  int n = 4;
  int z = 0;
  int i,j;

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

C++

#include <iostream.h>

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

Java

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


	  for (int i = 1; i <= n; i++)
	  {
		for (int j = 1; j < i * 2; j++)
		{
		  if (j <= i)
		  {
			System.out.print((j + z)+" ");
		  }
		  else
		  {
			System.out.print((i * 2 - j + z)+" ");
		  }
		}
		z = z + i;
		System.out.println();
	  }
	  
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 4;
    int z = 0;


    for (int i = 1; i <= n; i++)
    {
      for (int j = 1; j < i * 2; j++)
      {
        if (j <= i)
        {
          Console.Write((j + z) + " ");
        }
        else
        {
          Console.Write((i * 2 - j + z) + " ");
        }
      }
      z = z + i;
      Console.WriteLine();
    }

    Console.ReadKey(true);
  }
}

Python

n = 4
z = 0

for x in range(1, n + 1):
  for y in range(1, x * 2):
      if y <= x:
         print(str(y + z) + " ", end="")
      else:
         print(str(x * 2 - y + z) + " ", end="")
  z = z + x
  print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments