6.27 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:

Respuesta :

Answer:

//Convert any decimal number to binary number

//Program is written in C++ Programming Language

// Comments are used for explanatory purpose

// Program starts here

#include <iostream>

using namespace std;

// Main Method declared here

int main()

{

    int x;

   cout<<"Enter any integer number: ";

    cin>>x;

   DecBin(x);

   return 0;

}

// Here a function named DecBin is declared along with an integer variable, x

void DecBin(int x)

{

   // Declare an array to store the resulting binary digits

   int bindigit[32];

   // counter for binary array

   int kount = 0;

   while (x > 0) {

       // Store the remainder of each division in the declared array

       bindigit[kount] = x % 2;

       x = x / 2;

       kount++;

   }

   // Loop to print the binary digits in reverse order

   for (int j = i - 1; j >= 0; j--)

          {

       cout << bindigit[j];

        }

}

// End of Program