Sunday, 27 January 2019

Binary Representation - Squares

The program must accept an integer N as the input. The program must print the binary representation of the square of each integer from 1 to N as the output.

Boundary Condition(s):
1 <= N <= 1000

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the binary representation of squares from 1 to N are separated by space(s).

Example Input/Output 1:
Input:
4
Output:
1 100 1001 10000

Explanation:
The square of 1 is 1. So the binary representation of 1 is 1.
The square of 2 is 4. So the binary representation of 4 is 100.
The square of 3 is 9. So the binary representation of 9 is 1001.
The square of 4 is 16. So the binary representation of 16 is 10000.
Hence the output is 1 100 1001 10000

Program

#include<stdio.h>
#include <stdlib.h>

int main()
{
   int N,a[1000],i,j,k=0;
   scanf("%d",&N);
   for(i=1;i<=N;i++)
   {
        j=i*i;
        k=0;
       while(j>0)
       {
           a[k++]=j%2;
           j=j/2;
       }
       for(k=k-1;k>=0;k--)
       printf("%d",a[k]);
       printf(" ");
   }

}

Output

9

1 100 1001 10000 11001 100100 110001 1000000 1010001