Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 9: Write a C++ program to convert a decimal number into binary using recursion.

#include<iostream>
using namespace std;

void d_b(int arr[],int num,int size)
{
	if(num==1)
	{
		arr[size-1]=1;
		return;
	}

	arr[size-1]=num%2;
	d_b(arr,num/2,size-1);
}

int main()
{
	int arr[8],n,flag=0;
	for(int i=0;i<8;i++)
	{
		arr[i]=0;
	}

	do{
		cout<<"\nEnter a number : ";
		cin>>n;
		cout<<"\n\n\t\tAfter Conversion\n\n";
		d_b(arr,n,8);
		cout<<"\t  ( "<<n<<" )  =  ( ";
		for(int i=0;i<8;i++)
		{
			cout<<arr[i];
		}
		cout<<" )";
		cout<<"\n\ndo you want to continue (yes-1 and no-0) : ";
		cin>>flag;
	}while(flag==1);

	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.