Hello everyone! Hope you all be fine.. I m trying to convert a decimal number to binary using a function. I tried to save the result into an array but the problem is i cannot display it backwards correctly.Please help
#include <iostream.h>
long decimal2binary(int); //Function protoype
main()
{
int dec;
cout<<"Please enter a decimal number: ";
cin>>dec;
decimal2binary(dec); //Function Call by value
}
long decimal2binary(int num) //Function definition
{
int i ,j, rem ,quo , bin[8] ;
for(j=0;j<=7;j++)
{
rem = num%2;
bin[8] = rem ;
num = num/2;
}
for(i=7;i>=0;i--) //Displaying the array backward
{
cout<<bin[i];
}
}