Pattern 315

Pattern 315 post thumbnail image

C

#include<stdio.h>
int main()
{
  int m=1;
  int p_height=5;
  int p_space=p_height-1;

  int i,j,k;  // loop variable


  int np=1; // for number printing
  char cp='A'; // for char printing

  for(i=1; i<=p_height; i++)
  {
    for(j=p_space; j>=i; j--)
    {
      printf(" ");
    }

    for(k=1; k<=m; k++)
    {
      if(i%2!=0)
      {
        printf("%d",np);  // print numbers if row is Odd
        np++;
      }
      else
      {
        printf("%c",cp); // print character if row is Even
        cp++;
      }

      np=(np>9)?1:np; //reset number
      cp=(cp>'Z')?'A':cp; //reset character
    }
    m+=2;
    printf("\n");
  }
}

C++

#include<iostream.h>
int main()
{
 int m=1;
 int p_height=5;
 int p_space=p_height-1;
 
 int i,j,k;  // loop variable


 int np=1; // for number printing
 char cp='A'; // for char printing                   
 
 for(i=1;i<=p_height;i++)
 {
  for(j=p_space;j>=i;j--)
  {
   cout<<" ";
  }
  
  for(k=1;k<=m;k++)
  {
   if(i%2!=0)
   {
    cout<<np;  // print numbers if row is Odd 
    np++;
   } 
   else
   {
    cout<<cp; // print character if row is Even
    cp++;
   }
 
   np=(np>9)?1:np; //reset number
   cp=(cp>'Z')?'A':cp; //reset character
  }
  m+=2;
  cout<<endl;
 }
 return 0;  
}

Java

class PatternProg
{
   public static void main (String args[])
   {
    int m = 1;
    int p_height = 5;
    int p_space = p_height - 1;

    int i; // loop variable
    int j;
    int k;


    int np = 1; // for number printing
    char cp = 'A'; // for char printing

    for (i = 1;i <= p_height;i++)
    {
     for (j = p_space;j >= i;j--)
     {
      System.out.print(" ");
     }

     for (k = 1;k <= m;k++)
     {
      if (i % 2 != 0)
      {
      System.out.print(np);
      np++;
      }
      else
      {
      System.out.print(cp);
      cp++;
      }

      np = (np > 9)?1:np; //reset number
      cp = (cp > 'Z')?'A':cp; //reset character
     }
     m += 2;
     System.out.println();
    }
    
   }
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int m = 1;
    int p_height = 5;
    int p_space = p_height - 1;

    int i; // loop variable
    int j;
    int k;


    int np = 1; // for number printing
    char cp = 'A'; // for char printing

    for (i = 1; i <= p_height; i++)
    {
      for (j = p_space; j >= i; j--)
      {
        Console.Write(" ");
      }

      for (k = 1; k <= m; k++)
      {
        if (i % 2 != 0)
        {
          Console.Write(np);
          np++;
        }
        else
        {
          Console.Write(cp);
          cp++;
        }

        np = (np > 9) ? 1 : np; //reset number
        cp = (cp > 'Z') ? 'A' : cp; //reset character
      }
      m += 2;
      Console.WriteLine();
    }
    Console.ReadKey(true);
  }
}

Python

 p_height = 5  # size
p_space = p_height - 1
m = 1
np = 1  # for num printing
cp = 'A'  # for char printing

for x in range(1, p_height + 1):
    for y in range(p_space, x - 1, -1):
        print(" ", end="")
    for z in range(1, m + 1):
        if (x % 2 != 0):
            print(np, end="")  # 1
            np += 1
        else:
            print(cp, end="")  # 2
            cp = chr(ord(cp) + 1)  # 3
        np = 1 if (np > 9) else np  # 4
        cp = 'A' if (cp > 'Z') else cp  # 5
    m += 2
    print()

"""
1) Print numbers if row is Odd
2) Print characters if row is Even
3) Incrementing the character
4) Reset number
5) Reset character
"""
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns