Hi really need help. I'm new to programming and my teacher just gave a problem program "Conversion from any base to base 10" . So search in google and find this source code. I just combined the source code. This will run if it not combines. But I want this to be combined properly and I need this to be in character array and in for loop. So I really need about this :((((
#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;
}