Binary representation

PROBLEM :

Write a program to print Binary representation of a given number.

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.

Output:
Print binary representation of a number in 14 bits.

Constraints:
1 = T = 100
1 = N = 5000

Example:
Input:
2
2
5

Output:
00000000000010
00000000000101

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
void Binary_representation(int [],int ) ;
int main()
 {
int t,no,i ;
cin>>t ;
while(t--)
{
   cin>>no ;
 
   int arr[14]={0} ;
   Binary_representation(arr,no) ;
 
   for(i=0;i<14;i++)
       cout<<arr[i] ;
     
  cout<<endl ;
}
return 0;
}

void Binary_representation(int arr[],int no)
{
    int k,i,rem ;
    k=14 ;
    while(no!=0)
    {
        rem=no%2 ;
        arr[--k]=rem ;
        no=no/2 ;
    }
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )