CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


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

    Posted by: sait on 1/13/2008

    Contact sait Contact sait
    Category Functions in C Interview Questions
    Rating (0.4) By 1855 users
    Added on 1/13/2008
    Views 3246
    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

    #include<stdio.h>
    void main()
    {
    int x[10],n,s,d,i;
    clrscr();
    printf("enter the no. to convert to binary");
    gets(n);
    while(n!=0)
    {
    s=s%2;
    n=n/2;
    x[i]=s;
    i++;
    d=i;
    }
    printf("binary no is--------");
    for(i=d-1;i>=0;i--)
    {
    printf("%d",x[i]);
    }
    }



    Posted by: arun (hazaribag)    

    Contact arun (hazaribag) Contact arun (hazaribag)

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n,j,i,a[16];
    printf("enter no");
    scanf("%d",&n);
    for(i=0;i<=15;i++)
    {
    if(n<0)
    {
    a[0]=0;
    n=-n;
    }
    else
    {
    a[i]=0;
    }
    }
    for(j=15;j>=1;j--)
    {
    a[j]=n%2;
    n=n/2;
    }
    printf("Binary no is");
    for(j=0;j<=15;j++)
    printf("%d",a[j]);
    getch();
    }



    Posted by: Hanmandlu Junjuru    

    Contact Hanmandlu Junjuru Contact Hanmandlu Junjuru

    int d = 365;
    for(; d; d >>= 1)
    printf("%d ", d & 0x1);



    Posted by: rsnich    

    Contact rsnich Contact rsnich

    #include<stdio.h>
    #include<conio.h>
    void push(int r);
    int pop();
    int top=-1,a[20];
    void main()
    {
    int num,r;
    printf("enter a number");
    scanf("%d",&num);
    while(num>0)
    {
    r=num%2;
    push(r);
    num=num/2;
    }
    while(top>0)
    {
    r=pop();
    printf("%d",r);
    }
    void push(int r)
    {
    if(top==19)
    {
    printf("overflow");
    }
    else
    {
    top=top+1;
    a[top]=r;
    }
    }
    int pop()
    {
    int item;
    if(top==-1)
    {
    printf("underflow");
    }
    else
    {
    item=a[top];
    return(item);
    top--;
    }
    }



    Posted by: RENUKA    

    Contact RENUKA Contact RENUKA

    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

    User Options
    Sponsored Links


    Copyright ©2003-2014 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.015625 Seconds