I'm new to C and I'm trying to convert decimal to binary. The result is giving me an inverse number of what's required. I tried to apply modulo on the result like I saw on other forums but I still get the same result. Any help?
#include<stdio.h>
int main()
{
int number;
long int quotient, rem;
printf("Enter a number: ");
scanf("%d", &number);
quotient=number;
while (quotient!=0)
{
quotient=quotient/2;
rem=quotient%2;
printf("%ld", rem%10);
rem/=10;
}
}
I took an advice basing on a Print Function but still not sure if i understand it stills give me the same result. Please have a look.
#include<stdio.h>
void Print(int number,int base)
{
if (number >= base)
Print(number/base,base);
printf("%d",number%base);
}
int main()
{
int number;
long int quotient, rem;
printf("Enter a number: ");
scanf("%d", &number);
quotient=number;
while (quotient!=0)
{
quotient=quotient/2;
rem = quotient%2;
Print(rem, 2);
}
}
Just a small note because i forgot to say and I don't want you to go through the trouble. The idea os to not use arrays. Thanks