Ok so I try to do it this program by myself and I can't do it mysfelf. Im a new to this programming stuff. So I came here to ask help. Hope you can help me. My prof gave me program that will convert any base to bae 10. "Conversion from any Base to Base 10" it should be with char array. So how to do this? This is a C++ Language.

Hi heres my program and I don't know what went wrong. And I need this to be in char array and in loop. Sorry I'm just a newbie in programming hope you will correct my mistakes this would help me a lot.

#include<stdio.h>
#include<math.h>
#include<string.h>
int binary_decimal(int n);
int decimal_binary(int n);
int decimal_octal(int n);
int octal_decimal(int n);
void decimal_hex(int n, char hex[]);
int hex_decimal(char hex[]);
int main()
{
int n;
char c;
printf("Instruction:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal. \n");
printf("2. Enter alphabet 'b' to convert decimal to binary. \n");
scanf("%c",&c);
if (c =='d' || c=='D')
{
printf("Enter a binary number: ");
scanf("%d",&n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c=='B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}

int decimal_binary(int n) /* Function to convert decimal binary. */
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*1;
i*=10;
}
return binary;
}
int i,rem;

int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0; i=0; rem;
while (n!=0)
{
rem=n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
int n;
char c;
printf("Instruction:\n");
printf("1. Enter alphabet 'o' to convert decimal to octal. \n");
printf("2. Enter alphabet 'd' to convert octal to decimal.\n");
scanf("%c",&c);
if (c =='d' || c =='D')
{
printf("Enter an octal: ");
scanf("%d",&n);
printf("%d in octal = %d in decimal", n, octal_decimal(n));
}
if (c =='o' || c=='O')
{
printf("Enter a decimal number: ");
scanf("%d",&n);
printf("%d in decimal = %d in octal", n, decimal_octal(n));
}
return 0;
}
int decimal_octal(int n) /* Function to convert decimal to octal */
{
int rem, i=1, octal=0;
while (n!=0)
{
rem=n%8;
n/=8;
octal+=rem*i;
i*=10;
}
return octal;
}
int octal_decimal(int n) /* Function to convert octal to decimal */
{

int decimal=0, i=0; rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(8,i);
++1;
}
return decimal;
}
char hex[20],c;
int n;
printf("Instruction:\n");
printf("Enter h to convert decimal to hexadecimal:\n");
printf("Enter d to to hexadecimal number to decimal:\n");
printf("Enter a character: ");
scanf("%c",&c);
if (c=='h' || c=='H')
{
printf("Enter decimal number: ");
scanf("%d",&n);
decimal_hex(n,hex);
printf("Hexadecimal number: %s",hex);
}
if (c=='d' || c=='D")
{
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Decimal number: %d",hex_decimal(hex));
}
return 0;
}

void decimal_hex(int n, char hex[]) /* Function to convert decimal to hexadecimal. */
{
int i=0,rem;
while (n!=0)
{
rem=n%16;
switch(rem)
{
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
default:
hex[i]=rem+'0';
break;
}
++i;
n/=16;
}
hex[i]='\0';
strrev(hex); /* Reverse string */
}
int hex_decimal(char hex[]) /* Function to convert hexadecimal to decimal. */
{
int i, length, sum=0;
for(length=0; hex[length]!='\0';++length);
for(i=0; hex[i]!='\0';++i,--length)
{
if(hex[i]>='0' && hex[i]<='9')
sum+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
sum+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
sum+=(hex[i]=87)*pow(16,length-1);
}
return sum;
}
This article has been dead for over six months. Start a new discussion instead.