CoolInterview.com - World's Largest Collection of Interview Questions
 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 LANGUAGES C DETAILS
    Question :
    Write a Program to convert decimal to binary no.

    Posted by: sait on 1/13/2008

    Contact sait  Contact sait
    Category C Interview Questions
    Rating (0.0) By 0 users
    Added on 1/13/2008
    Views 1461
    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


    If you have the better answer, then send it to us. We will display your answer after the approval.
    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
    Can anyone share his code which would accept a sentence, then count the number occurrence of the words excluding spaces.All is done using linked list.
    View Answer
    using c-strings write a program that will analyse the text"1.1my brother is taller than me.1.2 I am a boy of sixteen years old". The program should remove multiple spaces betweem words,find the longest word in the text,search and identify the number of letters"e", extact the number of integers , extract the number of doubles, extract the number of words in each sentence and identify the number of sentences in the text.
    View Answer
    How do I use c-strings to write a c program that Removes multiple spaces between words in a paragraph?
    View Answer
    Write A Program With Out Using ;
    View Answer
    What are the applications and advantages of C-language?
    View Answer
    A variable carries a value.(already assigned OR from Scanf..what ever the way). I want to know whether the variable is positive or negative.
    Conditions:
    -----------
    1.NO looping logic should be used such as for ..while..etc.

    2.should not use > or <.

    3.should not use any numbers except 0.
    We can use 0 and equality operator.
    How to know the number is positive or negative?
    View Answer
    Define structural language and procedural language?
    View Answer
    Q.8 When a 'C' function call is made, the order in which parameters passed to the function are pushed into the stack is
    (a) left to right
    (b) right to left
    (c) bigger variables are moved first than the smaller variables.
    (d) smaller variables are moved first than the bigger ones
    (c) none of the above.
    View Answer
    how do i get started for any project?
    View Answer
    What is the output of
    void main()
    {
    int a = (1,2,3);
    printf("%d",a);
    }

    with reason.
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL C Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2009 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/12730/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | Joke of the Day

    0.47