Like us on google+

Thursday, 24 October 2013

Widgets

PROGRAM WHICH CONVERTS DECIMAL NUMBER TO BINARY WITHOUT USING ARRAYS AND RECURSION.

#include<stdio.h>
#include<math.h>
int decimaltobinary(int num);
int main()
{

    int num,binNum;
    printf("Enter the decimal number: ");
    scanf("%d",&num);
    binNum=decimaltobinary(num);
    printf("Binary value of %d is %d",num,binNum);
    return 0;
}

int decimaltobinary(int n)
{
    int b=0,c=0;
    while(n>0)
   { 
     if(n%2==1)
        {
         b+=pow(10,c);
        }
        c++;
        n=n/2;
    }
    return b;


}
-----------------------------------------------------
Sample output:
Enter the decimal number:15
Binary value of 15 is 1111
----------------------------------------------------
Description:
Step1: Getting the input from the user and storing the value in num.

Step2: calling decimaltobinary(num);function and passing num as an argument.In the above program 15 is sent as argument.

Step3: Value of num is stored in variable n which of type integer and the variables b and c are initialized to 0.

Step4: Now the condition inside the loop is checked.
      while(15>0) its true ...

Step5: If reminder obtained after dividing the number by 2 is equal to 1 then the expression b+=pow(10,c) is executed and c is incremented and expression n=n/2 is also executed.

Step6: Suppose reminder obtained in step5 is 0 then only c is incremented and expression n=n/2 is executed.
Process continues until the condition (n>0) is true.

Step7: When condition fails value in b which is binary equivalent of the decimal number is returned to the calling function and printed on the screen.
----------------------------------------------------

Earlier I had posted solution for the same question but by using recursion.Understanding recursive functions is little bit complicated so I think this program is very easy to understand and also more efficient.

Click on the link to refer to the same program but by using recursion.

Hope its helpful............:)  

SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
About us:
Hi guys Sandesh and Rajesh here ... studing engineering in PESIT started this blog as a google contest and also we love blogging ...Hope u like it ...Encourage us by liking us on g+... Any queries dont hesitate to ask Read More →

1 comments: