patternprogram_397 softethics

Pattern 397

C

#include <stdio.h>

int main()
{
  int n = 10; // size (Even size)

  int px = n/2; // mid
  int py = n/2;


  int i,j; // loop var

  for(i = 1; i <= n; i++)
  {

    for(j = 1; j <= n-1; j++)
    {
      if((i==1 || i==n) && j==n/2)
      {
        printf("*");
      }
      else if(j==px || j == py)
      {
        printf("-");
      }
      else
      {
        printf(" ");
      }
    }

    if(i==n/2)
    {
      int t=px; // swap value of px and py
      px=py;
      py=t;
    }
    else
    {
      px--;
      py++;
    }

    printf("\n");
  }

  return 0;
}

C++

#include <iostream.h>

int main()
{
 int n = 10; // size (Even size)

 int px = n/2; // mid
 int py = n/2;


  // loop var

 for(int i = 1; i <= n; i++)
 {
  
  for(int j = 1; j <= n-1; j++)
  {
  if((i==1 || i==n) && j==n/2)
  {
    cout<<"*";	
  }
  else if(j==px || j == py)
   {
    cout<<"-";	
   }
   else
   {
    cout<<" ";
   }
  }
 
   if(i==n/2) 
   {
     int t=px; // swap value of px and py
	 px=py;
     py=t;
   }
   else
  {
  px--;  
  py++;
  }
  
  cout<<endl;
 }

return 0;
}

Java

class PatternProg
{
	public static void main(String args[])
	{
	 int n = 10; // size (Even size)

	 int px = n / 2; // mid
	 int py = n / 2;


	  // loop var

	 for (int i = 1; i <= n; i++)
	 {

	  for (int j = 1; j <= n - 1; j++)
	  {
	  if ((i == 1 || i == n) && j == n / 2)
	  {
		System.out.print("*");
	  }
	  else if (j == px || j == py)
	  {
		System.out.print("-");
	  }
	   else
	   {
		System.out.print(" ");
	   }
	  }

	   if (i == n / 2)
	   {
		 int t = px; // swap value of px and py
		 px = py;
		 py = t;
	   }
	   else
	   {
	  px--;
	  py++;
	   }

	  System.out.println();
	 }

	
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 10; // size (Even size)

    int px = n / 2; // mid
    int py = n / 2;


    // loop var

    for (int i = 1; i <= n; i++)
    {

      for (int j = 1; j <= n - 1; j++)
      {
        if ((i == 1 || i == n) && j == n / 2)
        {
          Console.Write("*");
        }
        else if (j == px || j == py)
        {
          Console.Write("-");
        }
        else
        {
          Console.Write(" ");
        }
      }

      if (i == n / 2)
      {
        int t = px; // swap value of px and py
        px = py;
        py = t;
      }
      else
      {
        px--;
        py++;
      }

      Console.WriteLine();
    }


    Console.ReadKey(true);

  }
}

Python

n = 10  # size
px = n // 2  # mid
py = n // 2

for x in range(1, n + 1):
  for y in range(1, n):
    if (x == 1 or x == n) and y == n // 2:
       print("*", end="")
    elif y == px:
       print("-", end="")
    elif y == py:
       print("-", end="")
    else:
       print(" ", end="")

  if x == n // 2:
     t = px
     px = py
     py = t
  else:
     px -= 1
     py += 1
  print()
5 2 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments