A Simple C program to convert DECIMAL to BINARY




A Simple C program to convert DECIMAL to BINARY



 1  #include<iostream>
 2  #include<stdio.h>
 3  using namespace std;
 4  int main()
 5  {
 6      int n, arr[100] ;
 7      int i=0,j;
 8      cout<<"Enter the decimal number --->  " ;
 9      cin>>n;
10      // using while loop to store in arr[]
11      while( (n/2) >=1)
12      {
13          arr[i++]= (n%2);
14          n=n/2;
15      }
16      if(n==3)
17      {
18          arr[i++]=1;
19          arr[i]=1;
20      }
21      else if(n==2)
22          arr[i]=0;
23      else if(n==1)
24          arr[i]=1;
25 
26      cout<<endl ;
27      for(j=i;j>=0;j--)
28          cout<<arr[j];
29 
30  return 0;
31  }



OUTPUT

Comments

Popular posts from this blog

CodeChef : Breaking Bricks || Problem Code: BRKBKS

HackerRank Problem : Reverse and capitalise first alphabet of each word.

LeetCode: 169 Majority Element