Problem Statement:
A positive integer is entered through the keyboard, write a
function to find the binary equivalent of this number using
recursion.
function to find the binary equivalent of this number using
recursion.
Solution:
//Ahmad Furqan
//P5.d
#include <iostream>
#include <conio.h>
using namespace std;
int num2bin_rec(int num)
{
//if number is less than 2 return it
if (num < 2)
return num;
//show num/2's binary equivalent
cout<<num2bin_rec(num/2);
//return 1st binary digit of number
return num % 2;
}
void main(void)
{
int num;
cout << "Enter a positive number: ";
cin >> num;
cout << num2bin_rec(num);
_getch();
}
No comments:
Post a Comment