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 ;
}
}
---------------------------------------------------------------------------------
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
Post a Comment