patternprogram_200 softethics

Pattern 200 (Binary No.Pattern)

C

#include <stdio.h>
long toBinary(int n);

int main()
{
  int n = 3, x = 1;
  int i,j;

  for(i = 1; i <= n; i++)
  {
    for (j = 1; j <= n; j++)
    {
      printf("%4d ",toBinary(x));
      x++;
    }
    printf("\n");
  }
  return 0;
}

long toBinary(int n)
{
  long bin = 0;
  int rem, i = 1, step = 1;
  while (n != 0)
  {
    rem = n % 2;
    n /= 2;
    bin += rem * i;
    i *= 10;
  }
  return bin;
}

C++

Java

C#

Python

5 2 votes
Rate this Program
Subscribe
Notify of
guest


0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments