hi, we have this assignment in our computer subject in school. we need to create a c program that converts decimals to base(2-9) and vice versa. i know how the coversion works but i cant perfect my program. here's my program... i know the output for the 1st case is wrong because it ends up showing in reverse and the last case wont work.

#include <stdio.h>                                                   
int main()
{
int n,r,b,sum; char c;
clrscr();;
printf("Enter base type:\nA-base 10 \nB-base R (2-9)");
scanf("%c",&c);
switch(c)
{         
case 'A':
case 'a':
 {printf("to what base (2-9)");
 scanf("%d",&b);
 printf("enter number:");
 scanf("%d",&n);
 for(sum=0;n>0; )
 {
 r=n%b;
 printf("%d", r);
 n/=b;}
 break;
}
case'B':
case'b':
 {printf("Enter base of number");
 scanf("%d",&b);
 printf("\nenter number:");
 for(scanf("%d",&n),sum=0;n>0; )
 {
 r=n%10;
 sum=sum+r;
 n=(n/b);
 n=(n*b);
 }
printf("converted number=%d", sum);
 }
}
return 0;
}

hope someone can help me.

Recommended Answers

All 4 Replies

>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.

i figured what was wrong with the 2nd case. it slipped from my mind... the first case, im still figuring out... ill go try what you posted..
thanks

Though I have not taken a closer look at you code I guess the problem is in the order in which the result is printed according to the first reply thread. I have also written a similar program that converts decimal numbers to base 2 and 8 i.e. binary and octal respectively. My biggest problem was also the order in which the result was printed. I solved it by the use of arrays. As you compute send the results one by one to array locations and then you will simply print the array in reverse. Array positions with no values assign zero so that it does not print garbage for the final result. Try that.
Ronny

my program has many errors. it only works for small numbers. anyways, i now know the correct program asking my classmates how they did theirs. i submitted a wrong program but at least i tried. thanks to Narue and Ronny for the reply...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.