Sunday, 17 November 2013

C++ Program to Find the Binary Value of Decimal Number Using for loop

#include<iostream.h>
#include <stdio.h>
int main()
{
  int n,x,a, c, k;

  cout<<"Enter an integer in decimal number system";
  cin>>x;
  n=x;
  cout<<"Binary Value OF Given Number Is: ";

 for( a=1;n!=0;a++)

  {
     n=n/2;

  }

a=a-2;
  for (c = a; c >= 0; c--)
  {
    k = x >> c;

    if (k & 1)
      cout<<"1";
    else
      cout<<"0";
  }
 return 0;
}

No comments:

Post a Comment