conversion from binary to decimal in c language - c

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));

Related

C program printing numbers with variable precision

Do you know how a number can be rounded up and printed with a precision that is not fixed by some natural number, but by some variable? If the user needs to enter how many decimal places to round, how to solve it?
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.2f", var);
return 0;
}
Here's simple approach that would work in your case:
You just need to put * before f, and that's it.
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.*f",r, var);
return 0;
}
You can put a * in place of the precision, in which case you can specify an int as the precision.
printf("%.*f", r, var);

Function to evaluate exponent

I wrote the following code in c to evaluate exponent of a number without using the math library
#include <stdio.h>
float powr(float,int);
int main(){
float a;
int b;
printf("Enter base and exponent a^b: ");
scanf("%.2f %d",&a,&b);
float p=powr(a,b);
printf("%.2f",p);
return 0;
}
float powr(float x,int y){
float r=1;
for(int i=1;i<=y;i++){
r=r*x;
}
return(r);
}
but no matter what base and exponent I input, the ouput always comes out to be 1.00. I can't find any mistake in this program and I tried running the powr function algorithm inside main() in a seperate program and it works.
In scanf() it onlys accepts field-width format, but no precision, see here.
You can just input the value, like this:
scanf("%f %d",&a,&b);
Also, you should always check the return value of scanf(). Something like this:
numOfItems = scanf("%.2f %d",&a,&b);
if(numOfItems != 2) // uh-oh
{
printf("Error while input!");
}

decimal to octal conversion in c programming (using code blocks)

Decimal to octal conversion c.using code blocks 16.01.the conversion works until 63.This is my first time posting here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// conversion of decimal to octal
int main()
{
int num,i,len=0,x[100];
printf("enter a number\t");
scanf("%d",&num);
while (num>0){
x[len]=num%8;
num=num/8;
len++;
}
for(i=len;i>=0;i--)
printf("\n%d",x[i]);
return 0;
}
This will work for the conversion from decimal to octal.
Note: Check the logic works!.Make the sum long int. After 63 your int no longer able to hold the answer.
The problem with the code is actually reported by the compiler if you turn on some warnings, however the problem is that you are implicitly converting from double to int, and depending on the implementation, you'd lose precision. pow returns a double but you're storing it in an int (x)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
/* ^^^^^^^^^^^^^^^^ problem here */
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
now the fix
Instead of using the library pow, you should implement your own that's integer only (because you KNOW you are using ints only)
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
Now to clean up the program a bit:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long pow10(int n);
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
int main()
{
int num, count;
long sum, x;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow10(count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%ld",sum);
return 0;
}

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