- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Binary representation of a given number in C++

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.

## Recursive method

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

### Algorithm

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.

### Example

#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); }

### Output

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

- Related Articles
- Number of leading zeros in binary representation of a given number in C++
- Binary representation of next number in C++
- Binary representation of previous number in C++
- Check if binary representation of a number is palindrome in Python
- Number of Steps to Reduce a Number in Binary Representation to One in C++
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- Prime Number of Set Bits in Binary Representation in C++
- Prime Number of Set Bits in Binary Representation in Python
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Sorting according to number of 1s in binary representation using JavaScript
- Array Representation Of Binary Heap
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- In MySQL, how we can get the corresponding string representation of the binary value of a number?
- Golang Program to check if the binary representation of a number is palindrome or not
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?

Advertisements