0

Not Yet Answered # Conversion from any base to base 10

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.

Discussion Starter john.kane.100483 -2 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

0

0

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.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```