Thursday, June 24, 2021
Home coding Write a program to convert decimal number to binary.

Write a program to convert decimal number to binary.

Given a decimal number we have to convert into binary number.

so what is binary number?

binary number is consist of 0 or 1.

Example:

input:4
output:100

input:9
output:1001

Logic:

step 1-divide number by 2 and store the remainder into array.
step 2-divide the number by 2
step 3-repeat the step 2 until number is greate than 0.

#include <iostream>  
using namespace std;  
int main()  
{  
int binary[10];
cout<<"Enter the number"<<endl;    
int n,i;
cin>>n;    
for(i=0;n>0;i++)    
{    
 binary[i]=n%2;  
 n= n/2;  
}    
cout<<"Binary Number: "<<endl;    
for(i=i-1;i>=0;i--)    
{    
cout<<binary[i];    
}    

} 

Output:

Enter the number:9
Binary Number: 
1001

Time Complexity:0(N)

space Complexity-0(N)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments