Sunday, March 16, 2014

Decimal to Binary converter (basic way)


Here is a basic way to get the binary value of a decimal number. In this program, a for loop is used which will get the remainder, decrease the number by dividing it by 2 and populate the array. At the end, we will simply print the array in reverse order.                                                                                                        
#include<iostream.h>

#include<conio.h>

void main()

{

int num,rem,div,i=0,a[100];

cout<<"Enter the number :  ";

cin>>num;

for (div=num/2;div>0;)

{

if (i==0)

 rem=num%2;

else

{                    //dividing the given number by 2 and getting remainder

rem=div%2;

div=div/2;

}

a[i]=rem;          //transfering remainder to array

i++;

}

cout<<endl<<"The Binary Value of given number is :  ";

for (int k=i-1;k>=0;k--)                      //printing array in reverse order

cout<<a[k];

getch();

}

Widgets

 

Copyright @ 2014 CPP Fuzz.