Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Programs
NEXT
 
(59 / 301)
 
 



Write a C program to convert a decimal number into a binary number.




99% of the people who attend interviews can't answer this question, believe me!. Here is a recursive C program which does this....


#include<stdio.h>
void generatebits(int num);

void generatebits(int num)
{
  int temp;
  if(num)
  {
    temp = num % 2;
    generatebits(num >>= 1);
    printf("%d",temp); 
  }
}

int main()
{
  int num;
  printf("\nEnter a number\n");
  scanf("%d", &num);
  printf("\n\n");
  generatebits(num);
  getch();
  return(0);
}



The reason we have shown a recursive algorithm is that, because of the magic of recursion, we dont have to reverse the bits generated to produce the final output. One can always write an iterative algorithm to accomplish the same, but it would require you to first store the bits as they are generated and then reverse them before producing the final output.


PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!