Hi all, I am trying to convert decimal to binary in c
the max decimal number is 10000 so any integer from 0 to 10000 can be entered
10000 in binary is 14 digits long
my function is as follows
int fun(int num)
{
int remain;
int x=0;
int sum=0;
int binary=0
while(x<13)
{
remain=10*(num%2); //so it will be in factors of 10
num=num/2; // keeps diving number by 2
if(num!=0)
{
sum=pow(remain,x)+sum;
}
x=x+1
}
binary=sum;
return binary;
}
however this only prints of sum =1 no matter what number is taken in
any ideas? I am trying to get it so if the remainder is 1 I multiply by 10 and raise it to the power of x, then add it to the sum
thanks for any insight. you guys are such a great group!
the max decimal number is 10000 so any integer from 0 to 10000 can be entered
10000 in binary is 14 digits long
my function is as follows
int fun(int num)
{
int remain;
int x=0;
int sum=0;
int binary=0
while(x<13)
{
remain=10*(num%2); //so it will be in factors of 10
num=num/2; // keeps diving number by 2
if(num!=0)
{
sum=pow(remain,x)+sum;
}
x=x+1
}
binary=sum;
return binary;
}
however this only prints of sum =1 no matter what number is taken in
any ideas? I am trying to get it so if the remainder is 1 I multiply by 10 and raise it to the power of x, then add it to the sum
thanks for any insight. you guys are such a great group!