CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Interview Success Kit - Get Success in Job Interviews Interview Success Kit - 30,000 Interview Que. & Ans.
Covers 30,000+ Interview Questions asked in actual MNC Interviews - all with model answers.
Get Your Dream Job Now - www.vyoms.com
Sponsored Links
Interview Questions
Our Services

INTERVIEW QUESTIONS C FUNCTIONS IN C DETAILS
Question :
Write a Program to convert decimal to binary no.

Posted by: sait on 1/13/2013

Contact sait Contact sait
Category Functions in C Interview Questions
Rating (3.1) By 38 users
Added on 1/13/2013
Views 1708
Rate it!
Answers:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc, char *argv[])
{
double num;
long dec;
double frac;
char decstr[1024], *decptr, fracstr[1024], *fracptr, *fracendptr;
printf("
Please enter a decimal number: ");
scanf("%lf", &num);
dec = (long)((long)num);
frac = num - (double)dec;

bzero(decstr, sizeof(decstr)); decptr = decstr + sizeof(decstr) - 1;
while(dec > 0 && decptr > decstr)
{
decptr--;
if(dec%2)
*decptr = '1';
else
*decptr = '0';
dec = dec/2;
}
if(decptr <= decstr)
{
printf("Oops...Buffer offerflow prevented. Terminating.");
exit(-1);
}
bzero(fracstr, sizeof(fracstr)); fracptr = fracstr; fracendptr = fracstr + sizeof(fracstr) - 1;
while(frac > 0 && fracptr < fracendptr)
{
frac = frac*2;
if((int)(frac))
*fracptr = '1';
else
*fracptr = '0';
frac = frac - (double)((int)frac);
fracptr++;
}
if(fracptr >= fracendptr)
{
printf("Oops...Buffer offerflow prevented. The fractional part may not be exactly accurate. But its pretty close.
");
}
printf("
Binary: %s.%s", decptr, fracstr);
return 0;
}



Posted by: Akhilesh Shirbhate

Contact Akhilesh Shirbhate Contact Akhilesh Shirbhate

#include<stdio.h>
void bin(int);
int main()
{int n;
printf("enter the decimal number you want to convert into binary");
scanf("%d",&n);
bin(n);
}

void bin(int n)
{ int k;
if(n<=1)
printf("%d",n);
else
{
k=n;
bin(n/2);
printf("%d",k%2);
}
}



Posted by: P.Karthikeyan

Contact P.Karthikeyan Contact P.Karthikeyan

#include<stdio.h>
main()
{
int i,j,n,b[20];
clrscr();
printf("enter the decimal number");
scanf("%d",n);
for(i=0;i<n;i++)
{
b[i]=n%2;
n=n/2;
}
for(j=n;j>0;j--)
{
printf("%d",b[j]);
}
getch();
}



Posted by: madhukar

Contact madhukar Contact madhukar

#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,ans=0;
printf("enter value to convert in binary");
scanf("%d",&a);
while(a!=0)
{
b=a%2;
a=a/2;
ans=ans*10+b;
}
printf("ans is %d",ans);
getch();
}


we can use long for big number



Posted by: rakesh chaudhary

Contact rakesh chaudhary Contact rakesh chaudhary

#include<stdio.h>
#include<conio.h>

void main()
{
int num,i,binary[20];
clrscr();

printf("
Enter a number:: ");
scanf("%d",&num);

for(i=0;i<16;i++)
{
binary[i]= (num>>(15-i) & 1)? 1 : 0;
}

printf("
Binary number is
");

for(i=0;i<16;i++)
printf("%d",binary[i]);

getch();
}



Posted by: Chethan

Contact Chethan Contact Chethan

#include<stdio.h>
void main()
{
int num,i,f,size;
int k;
int count=0;
int ass=15;

int arr[16];
printf("Enter your number : ");
scanf("%d",&num);
for(i=num;i>=1;i/=2)
{
f = i%2 ;
printf(" %d",f);
for(k=0;k<=ass;k++)
{
arr[k] = f;
}
ass = ass - 1;
count = count + 1;
size = 15-count;
for(k =0;k<=size;k++)
arr[k] = 0;

}
printf("
");
printf("
for a 16bit compiler
");

for(k=0;k<=15;k++)
printf(" %d ",arr[k]);


}



Posted by: pinak mishra

Contact pinak mishra Contact pinak mishra

let the no is n the the code for it is
int k=1,s=0,d;
while(n>0)
{
d=n%2;
s=s+d*k;
n=n/2;
k=k*10;
}
printf("%d",s);

s contains the binary equivalent of entered decimal no.



Posted by: malaram kumhar

Contact malaram kumhar Contact malaram kumhar

/*decimal to binary conversion*/
#include<stdio.h>
main()
{
long a,d,bin=0,b=1;
int rem;
printf("enter a decimel integer
");
scanf("%ld",&a);
d=a;
while(a>0)
{
rem=a%2;
bin=bin+rem*b;
a=a/2;
b=b*10;
}
printf("decimal integer number is=%ld
",d);
printf("binary equivalent is=%ld
",bin);
}



Posted by: AMBIKA.R.BIRADAR

Contact AMBIKA.R.BIRADAR Contact AMBIKA.R.BIRADAR

void showbits(int n)
{
int i = 0;
while(i < (sizeof(n) * 8))
{
printf("%d ",(n & ~(~0u>>1) ? 1 : 0));
n = n<<1;
i++;
}
}



Posted by: sunil

Contact sunil Contact sunil

#include <stdio.h>
int main()
{
unsigned int num = 12345;
unsigned int count = 1;

count = count << ((sizeof(int)*8) - 1);

printf("input: %d
output: ", num);

while (count)
{
if (num & count)
{
printf("1");
}
else
{
printf("0");
}
count = count >> 1;
}
printf ("
");

return 0;
}


sample output
==============
[aneebk@localhost interview]$ cc to_convert_dec_to_binary.c
[aneebk@localhost interview]$ ./a.out
input: 12345
output: 00000000000000000011000000111001
[aneebk@localhost interview]$



Posted by: Aneeb

Contact Aneeb Contact Aneeb

#include<stdio.h>
#include<conio.h>
bin(int no);
void main()
{
int no;
clrscr();
printf("enter decimal no
");
scanf("%d",&no);
bin(no);
getch();
}
bin(int no)
{
int a;
if(no==0)
{
return(1);
}
else
{
a=no%2;
no=no/2;
bin(no);
}
printf("%d",a);
}



Posted by: Neha Vora

Contact Neha Vora Contact Neha Vora

#include<stdio.h>
int a = 32;

for(i=0;i< (sizeof(a)*8) ; i++ ) {
printf("%d",(a<<i & 1<<31)?1:0);
}
return 0;
}



Posted by: prashanjit ghosh

Contact prashanjit ghosh Contact prashanjit ghosh

bits_number(int n)
{
int i,k;
for(i=15;i>=0;i--)
{
mask=1<<i;
k=n&mask;
k==0?printf("0"):printf("1");
}
}



Posted by: MS

Contact MS Contact MS

void main()
{
int n[20],x,i=0,rem,a,num=0;

printf("Enter The NUMBEr: ");
scanf("%d",&num);
while(num!=0)
{
rem=num%2;
num=num/2;
n[i]=rem;
i++;
a=i;
}
printf("
BINARY NO = ");
for(i=a-1;i>=0;i--)
{
printf("%d",n[i]);
}
}



Posted by: Rahul agrawal,Dhule

Contact Rahul agrawal,Dhule Contact Rahul agrawal,Dhule

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification Enter the above shown code: *
    Inform me about updated answers to this question

    Related Questions
    View Answer
    What is the output of
    void main()
    {
    int a = (1,2,3);
    printf("%d",a);
    }

    with reason.
    View Answer
    code To draw a three dimensional graph using c graphics
    View Answer
    What is meant by malloc function
    View Answer
    How do we get Square root of any number Without using sqrt() function?

    View Answer
    What are returned by printf(), scanf() functions,if they return anything means what are that?

    View Answer
    Have you heard of "mutable" keyword?

    View Answer
    What is the purpose of main( ) function?

    View Answer
    Is using exit() the same as using return?


    View Answer
    Is it possible to execute code even after the program exits the main() function?


    View Answer
    What is a static function?


    View Answer
    Difference between Funtion to pointer and pointer to function

    View Answer
    How can send unlimited no of arguments to a function, eg printf function can take any no of arguments

    View Answer
    We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()

    View Answer
    How argc and argv works in the following main function?


    main(int argc,char *argv[])
    { int n,i=0;
    while(argv[1][i]!='
    View Answer
    How to write a program such that it will delete itself after exectution?


    View Answer
    output of the following program
    void main()
    {
    unsigned i;
    i=100*400;
    printf(

    View Answer
    When function say abc() calls another function say xyz(), what happens in stack?

    View Answer
    In c , main() is a function . and where is defined main() in c. bcz every function has three parts.
    1>. decleration
    2>. definition.
    3>. calling

    View Answer
    How to write a C program to find the power of 2 in a normal way and in single step?

    View Answer
    What is the difference between goto and longjmp() and setjmp()?
    View Answer

    Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

    View ALL Functions in C Interview Questions & Answers

    User Options
    Latest 20 Questions
    How to create arrays in JavaScript?
    How can JavaScript codes be hidden from old browsers that don?t support JavaScript ?
    What is namespacing in JavaScript and how is it used?
    What are the decodeURI() and encodeURI()?
    How are DOM utilized in JavaScript?
    Write the point of difference between web-garden and a web-farm?
    What is the ?Strict? mode in JavaScript and how can it be enabled ?
    What are escape characters?
    How can convert the string of any base to integer in JavaScript?
    What is the difference between ViewState and SessionState?
    Write the code for adding new elements dynamically ?
    Between JavaScript and an ASP script, which is faster?
    What are JavaScript types ?
    What qualities do you look for in a boss?
    How you would be an asset to this company?
    Why should we hire you?
    What is project implementation?
    Differentiate Public and Private Accounting?
    Explain what is compound journal entry ?
    Differentiate between provision and reserve?


    Copyright ©2003-2016 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0 Seconds