- Related Questions & Answers
- Binary representation of next number in C++
- Binary representation of previous number in C++
- Prime Number of Set Bits in Binary Representation in C++
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- Number of Steps to Reduce a Number in Binary Representation to One in C++
- Check if binary representation of a number is palindrome in Python
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Prime Number of Set Bits in Binary Representation in Python
- Array Representation Of Binary Heap
- Sorting according to number of 1s in binary representation using JavaScript
- Circular Permutation in Binary Representation in C++
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- Queries on number of Binary sub-matrices of Given size in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

A **binary number** is a number that consists of only two digits 0 and 1. For example, 01010111.

There are various ways to represent a given number in binary form.

This method is used to represent a number in its binary form using recursion.

Step 1 : if number > 1. Follow step 2 and 3. Step 2 : push the number to a stand. Step 3 : call function recursively with number/2 Step 4 : pop number from stack and print remainder by dividing it by 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); }

The number is 6 and its binary representation is 110 The number is 12 and its binary representation is 1100

Advertisements