my program and when i try to convert from decimal to octal the first it give me the right answer
This is my program and when i try to convert from decimal to octal the first it give me the right answer, the second time it give me the new answer plus the first answer. for example when i type 10 it give me 12 then the second time i type in 10 it give me 1212. could you please help me.
#include
void decimaltobinary (void);
void decimaltooctal (void);
void decimaltohexa (void);
char binary[33]=""000000000000000000000000"";
char octal[12]=""00000000000"";
int remainder;
unsigned long decimal;
int n=0;
int i;
int choice;
unsigned long dec;
void main (void)
{
int choice;
do{
do{
printf(""1. decimal to binary\n2. decimal to octal\n3. decimal to hexa\n4. Quit\n"");
printf(""please enter a number: "");
scanf(""%d"", &choice);
printf(""\n"");
}while (choice<1||choice>4);
switch (choice)
{
case 1: decimaltobinary ();
break;
case 2: decimaltooctal ();
break;
case 3: decimaltohexa ();
break;
}
}while(choice!=4);
printf(""\n"");
}
void decimaltobinary (void)
{
printf(""enter a decimal number to be converted to binary: "");
scanf(""%d"", &decimal);
dec=decimal;
printf(""decimal is %ld: "", dec);
while(dec!=0)
{
remainder=dec%2;
dec=dec/2;
if (remainder==1)
{
binary[32-n]='1';
}
else
{
binary[32-n]='0';
}
++n;
}
for (i=0; binary[i]=='0'; ++i)
;// nothing here
for (; i<33; i++)
{
if ((i-1)%4==0)
{
printf("" "");
}
printf(""%c"", binary[i]);
}
printf("" in binary\n\n"");
}
void decimaltooctal (void)
{
char octal[12]=""00000000000"";
printf(""enter a decimal number to be converted to octal: "");
scanf(""%d"", &decimal);
dec=decimal;
printf(""decimal is %ld: "", dec);
while(dec!=0)
{
remainder=dec%8;
dec=dec/8;
switch (remainder)
{
case 0: octal[11-n]='0';
break;
case 1: octal[11-n]='1';
break;
case 2: octal[11-n]='2';
break;
case 3: octal[11-n]='3';
break;
case 4: octal[11-n]='4';
break;
case 5: octal[11-n]='5';
break;
case 6: octal[11-n]='6';
break;
case 7: octal[11-n]='7';
break;
}
++n;
}
for (i=0; octal[i]=='0'; ++i)
;// nothing here
for (; i<12; i++)
{
printf(""%c"", octal[i]);
}
printf("" in octal\n\n"");
/* octal[0]=' ';
octal[1]=' ';
octal[2]=' ';
octal[3]=' ';
octal[4]=' ';
octal[5]=' ';
octal[6]=' ';
octal[8]=' ';
octal[9]=' ';
octal[10]=' ';
octal[11]=' ';
octal[12]=' ';
octal[13]=' ';
octal[14]=' ';
octal[15]=' ';*/
}
void decimaltohexa (void)
{
printf(""enter a decimal number to be converted to hex: "");
scanf(""%d"", &decimal);
dec=decimal;
printf(""decimal is %ld: "", dec);
while(dec!=0)
{
remainder=dec%16;
dec=dec/16;
switch (remainder)
{
case 0: octal[11-n]='0';
break;
case 1: octal[11-n]='1';
break;
case 2: octal[11-n]='2';
break;
case 3: octal[11-n]='3';
break;
case 4: octal[11-n]='4';
break;
case 5: octal[11-n]='5';
break;
case 6: octal[11-n]='6';
break;
case 7: octal[11-n]='7';
break;
case 8: octal[11-n]='8';
break;
case 9: octal[11-n]='9';
break;
case 10: octal[11-n]='A';
break;
case 11: octal[11-n]='B';
break;
case 12: octal[11-n]='C';
break;
case 13: octal[11-n]='D';
break;
case 14: octal[11-n]='E';
break;
case 15: octal[11-n]='F';
break;
}
++n;
}
for (i=0; octal[i]=='0'; ++i)
;// nothing here
for (; i<12; i++)
{
printf(""%c"", octal[i]);
}
printf("" in octal\n\n"");
}

