octal or hexadecimal number to decimal in C - c

Scan a number along with its base value. If a number starts with 0 it is octal nd if it starts with 0x it is hexadecimal. print the corresponding decimal value.
#include<stdio.h>
int main()
{
int a,b;
scanf("%x %d",&a,&b);
printf("%d",a);`
}
3 test cases shows error .please help me with this.

Use scanf i flag:
Matches an optionally signed integer; the next pointer
must be a pointer to int. The integer is read in base 16
if it begins with 0x or 0X, in base 8 if it begins with 0,
and in base 10 otherwise. Only characters that correspond
to the base are used.
#include<stdio.h>
int main(void)
{
int a,b;
scanf("%i %i",&a,&b);
printf("%d %d\n", a, b);`
return 0;
}

#include<stdio.h>
int main(void)
{
int oc,hex;
scanf("%i",&oc);
scanf("%i",&hex);
printf("\ndecimal:%d",oc);
printf("\ndecimal:%d",hex);
return 0;
}
another way
#include<stdio.h>
int main()
{
int oc,hex;
printf("enter the octal value:");
scanf("%o",&oc);
printf("enter the hex value:");
scanf("%x",&hex);
printf("\noctal to decimal:%d",oc);
printf("\nhex to decimal:%d",hex);
}

Related

Why am I getting a character in output when my input is 5 or more than 5? It is pure mathematical equation. If anything is wrong please tell me

I have compiled this code and it works just fine up to value 4 then it starts returning character instead of integer. I am talking about first equation => x= num*2; Here when I enter num value as 5 the output returns a.
#include <stdio.h>
int main(void)
{
int num;
int x; This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num); //
//printf("%d\n", num);
x = num * 2 ;
printf("%x\n", x);
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
}
please tell me if there is a mistake I am a newbie to coding.
As I see you are learning C language, and after reading your explanation, I feel that you want to print the integer value of variable x.
Kindly replace %x with %d in the print statement of variable x,
and you will be successfully able to print the value.
#include <stdio.h>
int main(void)
{
int num;
int x; // This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num);
x = num * 2 ;
printf("%d\n", x); // %d for integer and %x for hexadecimal values
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
return 0;
}
Finally, do read more about format specifiers in scanf and
printf statements.

conversion from binary to decimal in c language

i want to convert from binary to decimal but convert function does not return any thing where is the problem ??
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int convert(int x); //fun to convert from binary to decimal
int main()
{
int x; //binary number
printf("plz enter binary number !\n\nbinary: ");
scanf("%d",&x);
printf("\ndecimal: ",convert(x));
return 0;
}
int convert(int x)
{
int sum=0; //decimal number
int i=0;
int r; //remainder
while(x!=0){
r=x%10;
sum+=r*pow(2,i);
x=x/10;
i++;
}
return sum;
}
You can also try
scanf("%i", &x);
printf("%d", x);
you should change your printf in main().
printf("\ndecimal: %d",convert(x));// add %d
Because to print a value by printf need to spacify the data type such as for int %d for character %c.
binary numbers are series of ones and zeros (0|1), if you are referring to this type of numbers then check this source code below. you may also find this link useful http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int bintodec(char bin[32]){
int dec=0;
while(*bin){
if((*bin!='0') && *bin!='1')
break;// only accept '0' & '1'
dec<<=1;
dec+=*bin-'0';
bin++;
}
return dec;
}
int main(){
char bin[32]=""; //i believe binary numbers are series of '1' and '0'
printf("plz enter binary number !\n\nbinary: ");
scanf("%s",bin);
printf("\ndecimal: %d\n",bintodec(bin));
return 0;
}
Use format specifier:
%d for integer (in this case)
%c for character
%f for float value
%s for string
%lf for double
%x for hexadecimal
In this case change print statement to:
printf("\ndecimal: %d",convert(x));

how to identify whether a number is a binary code or a decimal in C

I've tried making a calculator that first reads the input from the user and then decides wheter the input is in binary or in decimal and then converts it.
i got almost everything but can't come up with the identifier! here's the code that i got:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int a;
char num[100];
int number[100];
int decimal_binary (int n);
int binary_decimal (int n);
void errorcode(void);
bool bin, decim;
int main (){
bin (false);
decim (false);
printf ("enter a number: ");
gets (num);
a = strlen(num);
if (a > 8){
errorcode();
//printf("That is an invalid number!");
//exit(45);
}
for (int i=0; i < a; i++){
number[i] = num[i] -'0';
printf("%d", number[i]);
}
int decimal_binary(int n){
int rem, i=1, binary = 0;
while (n != 0)
{
rem =n%2;
n/=2;
binary+=rem *i;
i*=10;
}
return binary;
}
int binary_decimal(int n)
{
int decimal = 0, i=0, rem;
while (n!-0){
rem = n%10;
n/=10;
decimal += rem *pow(2,i);
++i;
}
return decimal;
}
}
void errorcode(void){
printf("That is an invalid number!");
exit(45);
}
There is no such way to detect whether the number entered was binary or decimal just by analysing the characters of the number entered. However, if you study the Microsoft Windows calculator application that ships along with the OS, the "programmer" type of calculator takes an input whether the entered number is binary or decimal from the user, then internally in its code it would check whether the entered number was a valid binary or decimal number.
In short, your code will have to tell the user to specify what number is entered.
I would recommend studying the Microsoft calculator application's "programmer" type calc in order to help you understand what you want to achieve.
I believe the correct answer to this question is that binary numbers can be identified with the 0b prefix. Ex: 0b11111111 is the byte 0xFF, or 255 decimal. Notice the 0x prefix on the hex number. Decimal numbers have no prefix.. In fact, if you lead your 'decimal' number with a zero, the compiler will think that it is an octal number and treat it as base 8.

Print a number to variable number of decimal places

I wanted to print a number to variable number of decimal places in C.
I have written the code
#include<stdio.h>
main()
{ int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.af", a, 2.554648);
return 0;
}
Use * in printf() to mark how many decimal places you want:
#include <stdio.h>
int main(void)
{
int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.*f", a, a, 2.554648);
return 0;
}
You need the * format specifier. Here is a short example(see in ideone):
#include <stdio.h>
int main(void) {
int a = 5;
double temp = 5.0 / 7;
printf("%.*f",a, temp);
return 0;
}

Converting a hexadecimal to binary

Im trying to figure out why this code only takes in hex numbers with 2 digits. For example, if I enter, "11" it will output, "00010001" but if I enter, "111" then it gives me some random number. I would like to try to make it accept as many digits as the user wants.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
void binary_hex(int n, char hex[]);
int hex_binary(char hex[]);
int main()
{
char hex[20],c;
int n;
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Binary number: %d",hex_binary(hex));
system("pause");
return 0;}
//Function to convert hexadecimal to binary.
int hex_binary(char hex[]) {
int i, length, decimal=0, binary=0;
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
decimal+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
decimal+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
decimal+=(hex[i]-87)*pow(16,length-1);
}
//At this point, variable decimal contains the hexadecimal number in decimal format.
i=1;
while (decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
return binary;
}
You need to use an array to store the binary number, as an int variable won't be able to store a large number, the range of int is(usually) from −32767 to +32767. C data types.
For example:
#include <stdio.h>
#include <math.h>
int hex_binary(char hex[], int binary_number[]);
int main()
{
char hex[20];
int binary_number[100];
int j,k;
printf("Enter hexadecimal number: ");
scanf("%19s",hex);
j = hex_binary(hex,binary_number);
printf("Binary number is: ");
for(k=j-1;k>=0;k--)
printf("%d",binary_number[k]);
printf("\n");
return 0;
}
//Function to convert hexadecimal to binary.
int hex_binary(char hex[], int binary_number[])
{
int i, j=0, length, decimal=0;
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
decimal+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
decimal+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
decimal+=(hex[i]-87)*pow(16,length-1);
}
//At this point, variable decimal contains the hexadecimal number in decimal format.
while (decimal!=0)
{
binary_number[j++] = decimal%2;
decimal/=2;
}
return j;
}
Output
Enter hexadecimal number: 111
Binary number is: 100010001
Enter hexadecimal number: 1111
Binary number is: 1000100010001
Enter hexadecimal number: 11111
Binary number is: 10001000100010001
Enter hexadecimal number: 987634
Binary number is: 100110000111011000110100

Resources