बाइनरी संख्या
बाइनरी संख्या केवल 0 और 1 अंकों से बनी होती है।
पुनरावर्ती विधि
पुनरावृत्ति का उपयोग करके संख्या को बाइनरी में बदलें:
- यदि संख्या > 1, तो संख्या/2 के साथ पुनरावर्ती कॉल करें
- वापस आते समय संख्या % 2 प्रिंट करें
उदाहरण कोड
#include <iostream>
using namespace std;
void toBinary(unsigned number) {
if (number > 1)
toBinary(number / 2);
cout << number % 2;
}
int main() {
int n = 6;
cout << "The number is " << n << " and its binary representation is ";
toBinary(n);
n = 12;
cout << "\nThe number is " << n << " and its binary representation is ";
toBinary(n);
return 0;
}
आउटपुट
The number is 6 and its binary representation is 110 The number is 12 and its binary representation is 1100