# Bases Converting in C

Page 1 of 1

## 8 Replies - 1837 Views - Last Post: 08 November 2007 - 12:49 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=36927&amp;s=e71d8e970cb9a1a4d6b47b1c5aa3fc72&md5check=' + ipb.vars['secure_hash'], cur_rating: 1, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

# Bases Converting in C

Posted 06 November 2007 - 11:33 AM

here i have the code for converting binart to decimal :
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Function Prototypes */
int bintodec(int fbase, int number);

int main()
{
int number;
int fbase;
int tobase;
int result;

printf("Enter base from: ");
scanf("%d",&fbase);

scanf("%d",&number);

printf("Enter base to convert to: ");
scanf("%d",&tobase);

result = bintodec(fbase,number);

printf("The result is:%d", result);

system("PAUSE");
return 0;

}

int bintodec(int fbase, int number)
{
int bin[256];
int counter =0;
int counterR =0;
int quo =1;
int result =0;
int result1 =0;
int res[256];
static int sum =0;

while(quo>0)
{
quo= number/10;
bin[counter] = number%10;
number=quo;
counter++;
}

while (counter>0)
{
result = pow(fbase, (counter-1));
result1=result*bin[counter-1];
res[counterR]=result1;
counter--;
counterR++;
}

while (counterR > 0)
{
sum = sum + res[counterR-1];
counterR--;
}

return sum;

}

```

and here i also have decimal to binary:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int tobase=0;
int number =0;
int counter = 0;
int bin [25];
int quo = 1;

scanf("%d",&number);

printf("Enter base to convert to: ");
scanf("%d",&tobase);

while (quo!= 0)
{
quo= number/tobase;
bin[counter] = number%tobase;
counter ++;
number=quo;
}

while (counter > 0)
{
printf( "%d", bin[counter-1]);
counter --;
}

system("PAUSE");
return 0;
}

```

i need to get those 2 separate codes into one source code which will ask the user to either convert from binary to decimal vice versa .....please help me out this is due a day from now
.....i think i was in the wrong forum ...my apologies

Is This A Good Question/Topic? 0

## Replies To: Bases Converting in C

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Bases Converting in C

Posted 06 November 2007 - 11:43 AM

I'd just make each one a function, then use your main program as the driver to prompt the user for their choice. Have you tried this?

### #3 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

## Re: Bases Converting in C

Posted 06 November 2007 - 11:46 AM

Amadeus, on 6 Nov, 2007 - 11:43 AM, said:

I'd just make each one a function, then use your main program as the driver to prompt the user for their choice. Have you tried this?

nope i need help to do that

This post has been edited by nickyan: 06 November 2007 - 12:46 PM

### #4 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

## Re: Bases Converting in C

Posted 06 November 2007 - 01:48 PM

Can anyone help me if so please post with watever you can help me with

### #5 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

## Re: Bases Converting in C

Posted 06 November 2007 - 04:33 PM

i tried to combine both codes but it doesnt work ....help me out .....
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Function Prototypes */
int bintodec(int fmbase, int num);
int dectobin(int tobase, int num);

int main()
{
int num;
int fmbase;
int tobase;
int result;
int convert;
int counter = 0;
int bin [25];
int quo = 1;

printf("Enter 0 to convert to binary\n");
printf("Enter 1 to convert to decimal\n");
scanf("%d",&convert);

printf("Enter the value to be converted:");
scanf("%d",&num);

printf("Enter base to be converted from: ");
scanf("%d",&fmbase);

printf("Enter base to be converted to: ");
scanf("%d",&tobase);

if(convert ==0)
convert= dectobin();
else if (convert ==1)
convert= bintodec();
else
{
printf("Error in Input");
exit(1);
}
result = bintodec(fmbase,num);

printf("The result is:%d", result);

result = dectobin(tobase,num);

printf("The result is:%d", result);
int dectobin( int tobase, int num)
do (quo!= 0)
{
quo= num/tobase;
bin[counter] = num%tobase;
counter ++;
num=quo;
}

while (counter > 0)
{
printf( "%d", bin[counter-1]);
counter --;
}

int bintodec(int fmbase, int num)
{
int bin[256];
int counter;
int counterR;
int quo ;
int result =0;
int result1 =0;
int res[256];
int sum =0;

if(quo>0)
{
quo= num/10;
bin[counter] = num%10;
num=quo;
counter++;
}

while (counter>0)
{
result = pow(fmbase, (counter-1));
result1=result*bin[counter-1];
res[counterR]=result1;
counter--;
counterR++;
}

while (counterR > 0)
{
sum = sum + res[counterR-1];
counterR--;
}

return sum;

}

system("PAUSE");
return 0;

```

### #6 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

## Re: Bases Converting in C

Posted 07 November 2007 - 07:29 PM

This post has been edited by nickyan: 08 November 2007 - 12:05 PM

### #7 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

## Re: Bases Converting in C

Posted 08 November 2007 - 08:16 AM

This post has been edited by nickyan: 08 November 2007 - 12:06 PM

### #8 jjhaag

• me editor am smartastic

Reputation: 48
• Posts: 1,789
• Joined: 18-September 07

## Re: Bases Converting in C

Posted 08 November 2007 - 12:38 PM

Quote

Wow...that's vindictive. Unless you just mean delete the thread...

In any case, the purpose of a forum such as this is to both provide assistance to members looking for help, and to provide a repository of the process of working through a problem, so that it is available to help others in the future.

So chances are this thread isn't going to be deleted, since it might help someone else out in the future.

### #9 nickyan

Reputation: 0
• Posts: 8
• Joined: 06-November 07

## Re: Bases Converting in C

Posted 08 November 2007 - 12:49 PM

yes i understand that but this code is not working and it is fished up so please delete the thread