Pattern 244

Pattern 244 post thumbnail image

C

#include<stdio.h>

int main()
{
  int i,j,k;
  int n=5;//size

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

    k=(i%2==0)?2:1;

    for(j=1; j<=i; j++)
    {
      printf("%d ",k);
      k+=2;
    }
    printf("\n");
  }

}

C++

 #include<iostream.h>
 
 int main()
 {
   int i,j,k;
   int n=5;//size
 
 for(i=1;i<=n;i++)
 {
 
  k=(i%2==0)?2:1;
 
  for(j=1;j<=i;j++)
  {
  cout<<k<<" ";
  k+=2;
  }
      cout<<endl;
 }
 
 }

Java

  public class PatternProg
 
     public static void main(String agrs[]) {
         int i;
         int j;
         int k;
         int n = 5; //size
 
   for (i = 1; i <= n; i++) {
 
    k = (i % 2 == 0) ? 2 : 1;
 
   for (j = 1; j <= i; j++) {
   System.out.printf("%d ", k);
    k += 2;
    }
   System.out.print("\n");
         }
 
     }
 }

C#

using System;

public class PatternProg
{

  public static void Main(string[] agrs)
  {
    int i;
    int j;
    int k;
    int n = 5; //size

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

      k = (i % 2 == 0) ? 2 : 1;

      for (j = 1; j <= i; j++)
      {
        Console.Write(k + " ");
        k += 2;
      }
      Console.WriteLine();
    }
    Console.ReadKey(true);
  }
}

Python

n = 5  # size
for x in range(1, n + 1):
    if (x % 2 == 0):
        k = 2
    else:
        k = 1
    for y in range(1, x + 1):
        print(str(k) + " ", end="")
        k += 2
    print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns