C++ Program to Convert Decimal to Binary

C++ Program to Convert Decimal to Binary

Write C++ Program to Convert Decimal to Binary

// CPP Program to Convert Decimal to Binary

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int n, B[100], j;
	int ans = 0;

	cout << "Enter the Decimal number :--> "; //assume that we have entered 7
	cin >> n;

	int i = 0;

	while(n > 0)
	{
		B[i] = n % 2;			//to store the remainder in array
		n = n >> 1;			//Right shift the value of n
		i++;
	}

	cout << "The Binary number is :--> ";
	for(j = i - 1; j >= 0; j--)
	{
		cout << B[j];
	}

	return 0;
}

Output:

Enter the Decimal number :--> 89
The Binary number is :--> 1011001