954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Decimal system to Binary, Octal and Hexadecimal system Conversion..

#include <stdio.h>


main()
{

       int ctr, bin, quotient, deci=0, binary,octal,hexa;
    	float rem;
       char mark_magic;

clrscr();

do
{
	clrscr();
	quotient = 1;
	binary = 1;

	gotoxy(32,3); printf("-=< MAIN MENU >=-");
	gotoxy(15,5); printf("from any Number System to Decimal System conversion");
	gotoxy(5,6); printf("------------------------------------------------------------------------");
	gotoxy(5,7); printf("------------------------------------------------------------------------");
	gotoxy(26,9); printf("[A] Binary System");
	gotoxy(26,11); printf("[B] Octal System");
	gotoxy(26,13); printf("[C] Hexadecimal System");
	gotoxy(26,15); printf("[X] Exit");
	gotoxy(26,21); printf("what's your choice?: ");

	mark_magic = getche();
	
	clrscr();


	switch(toupper(mark_magic))

	 {
		case 'A':
			clrscr();

			gotoxy(10,5); printf("########^_^ Convert Decimal system to Binary system ^_^########");
			gotoxy(5,6); printf("------------------------------------------------------------------------");
			gotoxy(5,7); printf("------------------------------------------------------------------------");			
			binary = 2;
			
			gotoxy(28,9);printf("enter decimal number: ");

			scanf("%d",&deci);


			quotient = deci / binary;
			
			
			
			for(bin=1;bin>=15;bin++)
			{	
			bin = deeci%2;	
			gotoxy(28,11);printf("Equivalent in Binary is %.0f",bin);
			}

			gotoxy(51,24); printf("press any key to exit: ");
		     

		     getche();
		     clrscr();
		     break;

		  case 'B':
			clrscr();

			gotoxy(10,5); printf("########^_^ Convert Decimal system to Octal system ^_^########");
			gotoxy(5,6); printf("------------------------------------------------------------------------");
			gotoxy(5,7); printf("------------------------------------------------------------------------");
			octal = 8;
			

			gotoxy(28,9);printf("enter decimal number: ");

			scanf("%d",&deci);

			for (ctr = 1; ctr<=deci; ctr++)

			quotient = deci / octal;

			gotoxy(28,11);printf("Equivalent in Octal is %d",quotient);


			gotoxy(51,24); printf("press any key to exit: ");

			getche();
			break;

		 case 'C':

			clrscr();

			gotoxy(10,5); printf("########^_^ Convert Decimal system to Hexadecimal system ^_^########");
			gotoxy(5,6); printf("------------------------------------------------------------------------");
			gotoxy(5,7); printf("------------------------------------------------------------------------");
			hexa = 16;

			gotoxy(28,9);printf("enter decimal number: ");

			scanf("%d",&deci);

			for (ctr = 1; ctr<=deci; ctr++)

			quotient = deci / hexa;

                        gotoxy(28,11);printf("Equivalent in Hexadecimal is %d",quotient);

			gotoxy(51,24); printf("press any key to exit: ");

			getche();
			break;

			}
			}
while(toupper(mark_magic) != 'X');

}

this was rejected by my teacher,she said a lot of codes is missing. the problem is, I don't know what i'm going to write coz im new with C. Can you pls. help me with my problem?

thanks a lot!

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

When you run a program and the first choice shows nothing, it's safe to say it's lacking! :)

I re-worked Choice A, but the answer is being written out in reverse. You need to take the digits and first put them into a small array or LIFO buffer of some kind, and print them out from that array or buffer in last in, first out, ourder, so when the number is 3, the zero's are all printed out before the 1's.

Always highlight your code you want to post on the forum, and press the # sign, so it will be made to look more like code, and not regular forum text.

#include <stdio.h>


int main()  {
   int ctr, bin, quotient, deci=0, binary,octal,hexa, gar;
   float rem;
   char mark_magic;

   do   {
      clrscr();
      quotient = 1;
      binary = 1;

      gotoxy(32,3); printf("-=< MAIN MENU >=-");
      gotoxy(15,5); printf("from any Number System to Decimal System conversion");
      gotoxy(5,6); printf("------------------------------------------------------------------------");
      gotoxy(5,7); printf("------------------------------------------------------------------------");
      gotoxy(26,9); printf("[A] Binary System");
      gotoxy(26,11); printf("[B] Octal System");
      gotoxy(26,13); printf("[C] Hexadecimal System");
      gotoxy(26,15); printf("[X] Exit");
      gotoxy(26,21); printf("what's your choice?: ");

      mark_magic = getche();
      clrscr();
      switch(toupper(mark_magic))   {
         case 'A':
         clrscr();
         gotoxy(10,5); printf("########^_^ Convert Decimal system to Binary system ^_^########");
         gotoxy(5,6); printf("------------------------------------------------------------------------");
         gotoxy(5,7); printf("------------------------------------------------------------------------");
         binary = 2;
         gotoxy(28,9);printf("enter decimal number: ");

         scanf("%d",&deci);
         quotient = deci / binary;
         gotoxy(28,11);printf("Equivalent in Binary is ");
         for(bin=1; bin <=16; bin++)  {
            printf("%d",deci % 2);
            deci = deci / 2;
         }

         gotoxy(51,24); printf("press any key to exit: ");
         getche();
         clrscr();
         break;

         case 'B':
            clrscr();
            gotoxy(10,5); printf("########^_^ Convert Decimal system to Octal system ^_^########");
            gotoxy(5,6); printf("------------------------------------------------------------------------");
            gotoxy(5,7); printf("------------------------------------------------------------------------");
            octal = 8;
            gotoxy(28,9);printf("enter decimal number: ");
            scanf("%d",&deci);
            for (ctr = 1; ctr<=deci; ctr++)
               quotient = deci / octal;
            gotoxy(28,11);printf("Equivalent in Octal is %d",quotient);
            gotoxy(51,24); printf("press any key to exit: ");
            getche();
            break;
         case 'C':
            clrscr();
            gotoxy(10,5); printf("########^_^ Convert Decimal system to Hexadecimal system ^_^########");
            gotoxy(5,6); printf("------------------------------------------------------------------------");
            gotoxy(5,7); printf("------------------------------------------------------------------------");
            hexa = 16;
            gotoxy(28,9);printf("enter decimal number: ");
            scanf("%d",&deci);
            for (ctr = 1; ctr<=deci; ctr++)
            quotient = deci / hexa;
            gotoxy(28,11);printf("Equivalent in Hexadecimal is %d",quotient);
            gotoxy(51,24); printf("press any key to exit: ");
            getche();
            break;

      }
   }while(toupper(mark_magic) != 'X');
   gar = getchar(); gar++;
   return 0;
}


/*this was rejected by my teacher,she said a lot of codes is missing. the problem is, I don't know what i'm going to write coz im new with C. Can you pls. help me with my problem?
thanks a lot!
*/
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Step 1 would be to remove ALL the gotoxy() and clrscr() and any other decorative features until you've actually got the damn thing to work.

Adding eye candy to an already working program is trivial, compared with trying to maintain it whilst you're debugging the code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

a goto statement isnt tat good for programming..
avoid it if possible..
this program becomes pretty simple without em..

adarshcu
Junior Poster
121 posts since May 2008
Reputation Points: 19
Solved Threads: 26
 

When you run a program and the first choice shows nothing, it's safe to say it's lacking! :)

I re-worked Choice A, but the answer is being written out in reverse. You need to take the digits and first put them into a small array or LIFO buffer of some kind, and print them out from that array or buffer in last in, first out, ourder, so when the number is 3, the zero's are all printed out before the 1's.

Always highlight your code you want to post on the forum, and press the # sign, so it will be made to look more like code, and not regular forum text.

#include <stdio.h>


int main()  {
   int ctr, bin, quotient, deci=0, binary,octal,hexa, gar;
   float rem;
   char mark_magic;

   do   {
      clrscr();
      quotient = 1;
      binary = 1;

      gotoxy(32,3); printf("-=< MAIN MENU >=-");
      gotoxy(15,5); printf("from any Number System to Decimal System conversion");
      gotoxy(5,6); printf("------------------------------------------------------------------------");
      gotoxy(5,7); printf("------------------------------------------------------------------------");
      gotoxy(26,9); printf("[A] Binary System");
      gotoxy(26,11); printf("[B] Octal System");
      gotoxy(26,13); printf("[C] Hexadecimal System");
      gotoxy(26,15); printf("[X] Exit");
      gotoxy(26,21); printf("what's your choice?: ");

      mark_magic = getche();
      clrscr();
      switch(toupper(mark_magic))   {
         case 'A':
         clrscr();
         gotoxy(10,5); printf("########^_^ Convert Decimal system to Binary system ^_^########");
         gotoxy(5,6); printf("------------------------------------------------------------------------");
         gotoxy(5,7); printf("------------------------------------------------------------------------");
         binary = 2;
         gotoxy(28,9);printf("enter decimal number: ");

         scanf("%d",&deci);
         quotient = deci / binary;
         gotoxy(28,11);printf("Equivalent in Binary is ");
         for(bin=1; bin <=16; bin++)  {
            printf("%d",deci % 2);
            deci = deci / 2;
         }

         gotoxy(51,24); printf("press any key to exit: ");
         getche();
         clrscr();
         break;

         case 'B':
            clrscr();
            gotoxy(10,5); printf("########^_^ Convert Decimal system to Octal system ^_^########");
            gotoxy(5,6); printf("------------------------------------------------------------------------");
            gotoxy(5,7); printf("------------------------------------------------------------------------");
            octal = 8;
            gotoxy(28,9);printf("enter decimal number: ");
            scanf("%d",&deci);
            for (ctr = 1; ctr<=deci; ctr++)
               quotient = deci / octal;
            gotoxy(28,11);printf("Equivalent in Octal is %d",quotient);
            gotoxy(51,24); printf("press any key to exit: ");
            getche();
            break;
         case 'C':
            clrscr();
            gotoxy(10,5); printf("########^_^ Convert Decimal system to Hexadecimal system ^_^########");
            gotoxy(5,6); printf("------------------------------------------------------------------------");
            gotoxy(5,7); printf("------------------------------------------------------------------------");
            hexa = 16;
            gotoxy(28,9);printf("enter decimal number: ");
            scanf("%d",&deci);
            for (ctr = 1; ctr<=deci; ctr++)
            quotient = deci / hexa;
            gotoxy(28,11);printf("Equivalent in Hexadecimal is %d",quotient);
            gotoxy(51,24); printf("press any key to exit: ");
            getche();
            break;

      }
   }while(toupper(mark_magic) != 'X');
   gar = getchar(); gar++;
   return 0;
}


/*this was rejected by my teacher,she said a lot of codes is missing. the problem is, I don't know what i'm going to write coz im new with C. Can you pls. help me with my problem?
thanks a lot!
*/


wow! thank you very much!! it is very helpful to me..

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

what is the code of this:


----------------------------------------

This program will convert octal to hexa:

Enter a number: ----

What Would you like to do, Press O for octal and H for hexa: -----

The Answer is: -----

Would you like to convert again(Y/N): ----


thanks

noelaligoy
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

amazing !

i didnt understand a single thing !

hahhahah

can you give a written explaination ?

ANGELxDEE
Newbie Poster
2 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Wow! a million Thanks to all..

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You