Bases Converting in C

converting decimal to binary vice versa

Page 1 of 1

8 Replies - 1300 Views - Last Post: 08 November 2007 - 12:49 PM Rate Topic: *---- 1 Votes

#1 nickyan  Icon User is offline

  • New D.I.C Head

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

Bases Converting in C

Post icon  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);
  
  printf("Enter your number:");
  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;
	
   printf("Enter your number:");
  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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 nickyan  Icon User is offline

  • New D.I.C Head

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

Re: Bases Converting in C

Posted 06 November 2007 - 11:46 AM

View PostAmadeus, 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

Was This Post Helpful? 0
  • +
  • -

#4 nickyan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 nickyan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

#6 nickyan  Icon User is offline

  • New D.I.C Head

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

Re: Bases Converting in C

Posted 07 November 2007 - 07:29 PM

PLEASE DELETE THIS FORUM TOPIC

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

Was This Post Helpful? 0
  • +
  • -

#7 nickyan  Icon User is offline

  • New D.I.C Head

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

Re: Bases Converting in C

Posted 08 November 2007 - 08:16 AM

PLEASE DELETE THIS WHOLE FORUM!!!!!ASAP

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

Was This Post Helpful? 0
  • +
  • -

#8 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Bases Converting in C

Posted 08 November 2007 - 12:38 PM

Quote

PLEASE DELETE THIS WHOLE FORUM!!!!!ASAP

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.
Was This Post Helpful? 0
  • +
  • -

#9 nickyan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1