how to convert binary to decimal and decimal to binary in C
lanaguage
Answers were Sorted based on User's Feedback
Answer / rahul
[decimal to binary]
#include<stdio.h>
#include<conio.h>
void main()
{
int dec,bin,a[100],i=0,r,j;
printf("enter the decimal number: ");
scanf("%d",&dec);
while(dec>0)
{
r=dec%2;
dec=dec/2;
a[i]=r;
i++;
}
printf("the equivalant binary numberis: ");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch()
}
Is This Answer Correct ? | 72 Yes | 32 No |
Answer / saravana priya
#include <stdio.h>
void main()
{
int num, bnum, dec = 0, base = 1, rem ;
printf(“Enter a binary number(1s and 0s)\n”);
scanf(“%d”, &num); /*maximum five digits */
bnum = num;
while( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}
printf(“The Binary number is = %d\n”, bnum);
printf(“Its decimal equivalent is =%d\n”, dec);
} /* End of main() */
Is This Answer Correct ? | 25 Yes | 9 No |
Answer / fazlur rahaman naik
u can use bitwise operators and some logical thinking.
Is This Answer Correct ? | 26 Yes | 15 No |
Answer / ruchi
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,r,s=0,i=0;
printf("\nEnter the decimal number ");
scanf("%d",&n);
while(n>0)
{
r=n%2;
n=n/2;
s=s+r*pow(10,i++);
}
printf("\nThe binary equivalent is ");
printf("%d",s);
getch();
}
Is This Answer Correct ? | 27 Yes | 17 No |
Answer / sheela
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,r,s=0,i=0;
printf("\nEnter the binary number ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
n=n/10;
s=s+r*pow(2,i++);
}
printf("\nThe binary equivalent is ");
printf("%d",s);
getch();
}
Is This Answer Correct ? | 10 Yes | 2 No |
Answer / rahul
//it is to convert Binary to decimal;
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int a[20],i,n,count=0,b[20],c[20],sum=0;
printf("ENter the number in binary form=\t");
scanf("%ld",&n); // Get a binary number
from the user
for (i=0;n>=1;i++)
{
a[i]=n%10;
n=n/10; // Loop To reverse the number And put
all reversed numbers in arry a[i]
count=count + 1; // count to count the number of times
this loop runs
}
for (i=0;i<=count-1;i++) // count -1 condition is used to
run the loop till the previous loop run
{
b[i]=pow(2,i); // This is to raise the power of 2 to no
of times previous loop runned.
}
for (i=0;i<=count-1;i++)
{
c[i]=a[i] * b[i]; // Multiply a[i] or reveresed binary
no with b[i] or increasing pow of 2 to count-1
sum=sum +c[i]; // it is to add the c[i] elements with
each other n put into sum variable.
}
printf("Decimal form =%ld",sum); // printing the sum to get
the decimal form
getch();
}
// Hope it solves your problem, ANy more assistance
honey.gupta13@yahoo.com
Is This Answer Correct ? | 15 Yes | 9 No |
Answer / naveen bhatt
#include<stdio.h>
#include<conio.h>
void main()
{
int i,r,d,num;
for(i=1;i<=num;i++)
r=num%2;
num=d*num/2;
printf("\n the valueis=%d",r);
getch();
}
Is This Answer Correct ? | 4 Yes | 9 No |
Tell me when is a void pointer used?
What is #line in c?
What is sorting in c plus plus?
What is variable in c example?
What is pointer & why it is used?
Why does this code crash?
What is data type long in c?
You are to write your own versions of strcpy() and strlen (). Call them mystrcpy() and mystrlen(). Write them first as code within main(), not as functions, then, convert them to functions. You will pass two arrays to the function in the case of mystrcpy(), the source and target array.
accept character from keyboard untill the user presses the enter key.If the user enters any character other than upper case(A-Z)alphabets program should stop taking any input
What is typeof in c?
How will you write a code for accessing the length of an array without assigning it to another variable?
Why is this loop always executing once?