>i know the output for the 1st case is wrong because it ends up showing in reverse
You're extracting the digits from least significant to most significant, and printing in the same order. Of course it'll end up showing in reverse. You need to figure out a way to store the digits so you can print them in the correct order, or extract the digits from most significant to least significant. For example:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
int value;
fputs ( "Enter an integer: ", stdout );
if ( scanf ( "%d", &value ) == 1 ) {
double temp = value;
int i;
fputs ( "The digits are: ", stdout );
for ( i = 0; (int)temp != 0; i++ )
temp /= 10;
while ( --i >= 0 ) {
temp *= 10;
printf ( "%d ", abs ( (int)temp % 10 ) );
temp -= (int)temp;
}
putchar ( '\n' );
}
return 0;
}
>and the last case wont work
Of course not. If you get a calculator and do a manual run of the conversion loop, you'll see why too. What you need to do is prepend the extracted digit to your conversion value (because you're extracting the digits in reverse) using something like this:
int j;
for ( j = 0; n != 0; j++ ) {
r = n % b;
n /= b;
sum += r * (int)pow ( 10, j );
}
Alternatively, you could extract the digits from most significant to least significant and append them to the conversion value.