Pattern 150

Pattern 150 post thumbnail image

C

#include<stdio.h>

int main()
{
  int n=5; //size
  int i,j;
  int p=1,c=1;

  for (i=1; i<=n; i++)
  {
    for (j=n-1; j>=i; j--)
    {
      printf(" ");
    }
    p = 1;
    printf("%d",p);
    while (c<i)
    {
      printf("%d",p*=2);
      c++;
    }
    while (c>1)
    {
      printf("%d",p/=2);
      c--;
    }
    printf("\n");
  }
  return 0;
}

C++

#include<iostream.h>

int main()
{
    int n=5; //size
    int i,j;
    int p=1,c=1;

    for (i=1; i<=n; i++)
    {
        for (j=n-1; j>=i; j--)
        {
            cout<<" ";
        }
        p = 1;
        cout<<p;
        while (c<i)
        {
            cout<<(p*=2);
            c++;
        }
        while (c>1)
        {
            cout<<(p/=2);
            c--;
        }
        cout<<endl;
    }
    return 0;
}

Java

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

        int p = 1;
        int c = 1;

        for (int i = 1; i <= n; i++)
        {
            for (int j = n - 1; j >= i; j--)
            {
                System.out.print(" ");
            }
            p = 1;
            System.out.print(p);
            while (c < i)
            {
                System.out.print((p *= 2));
                c++;
            }
            while (c > 1)
            {
                System.out.print((p /= 2));
                c--;
            }
            System.out.println();
        }

    }
}

C#

using System;

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

    int p = 1;
    int c = 1;

    for (int i = 1; i <= n; i++)
    {
      for (int j = n - 1; j >= i; j--)
      {
        Console.Write(" ");
      }
      p = 1;
      Console.Write(p);
      while (c < i)
      {
        Console.Write((p *= 2));
        c++;
      }
      while (c > 1)
      {
        Console.Write((p /= 2));
        c--;
      }
      Console.WriteLine();
    }
    Console.ReadKey(true);
  }
}

Python

n=4 #size
p=1
c=1
for x in range(1, n+1):
  for y in range(n-1, x-1, -1):
    print(" ", end="")
  p=1
  print(p, end="")

  while (c<x):
   p *= 2
   print(p,end="")
   c += 1

  while (c > 1):
    p //= 2
    print(p, end="")
    c -= 1

  print()
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns