Need help with this program for a simple decimal to octal converter. I'm using Code::Blocks 16.01 IDE with GNU GCC Compiler.
An input of 77 should produce 115 but I get 114. "oct = oct + rem * pow(10,p);" is not producing the correct output consistently. It's a digit less at times. Could this be a linker issue to the math.h header? Not sure what else to check.
Thank you.
#include <stdio.h>
#include <math.h>
int main()
{
int n1,n2,rem,oct,p;
printf("Enter any number: ");
scanf("%d",&n1);
n2=n1;
p=oct=0;
while (n1>0)
{
rem = n1 % 8;
n1 = n1 / 8;
oct = oct + rem * pow(10,p);
++p;
}
printf("The octal equivalent of %d is %d\n", n2,oct);
return 0;
}