Introduction

Control Statement

- Program to check vowel or constant
- Program to check the largest number among three numbers
- Program to the sum of natural numbers
- Program to check leap year
- Program to find factorial of the number
- Program to find reverse of the number
- Program to Check Armstrong Number
- Program to find the sum of Natural Numbers using Recursion
- Calculator Program

Function

- Program to convert Binary to Decimal Number
- Program to convert Decimal to Binary Number
- Program to convert Binary to Octal Number
- Program to convert Octal to Binary Number
- Program to find reverse of the sentence using Recursion
- Program to Shutdown and restart Computer
- Program to find the area of triangle
- Program to find LCM
- Program to check prime number using Functions

Array

Pointer

OOPS

No Examples found for this topic - CodeHelpPro

# Program to convert Decimal to Binary Number

In this example, We will learn how to convert decimal number to binary number.

To understand this example, You must have knowledge in following topics

```
#include <iostream>
#include <cmath>
using namespace std;
int convertBinaryToDecimal(long long);
int main()
{
long long n;
cout << "Enter a binary number: ";
cin >> n;
cout << n << " in binary = " << convertBinaryToDecimal(n) << " in decimal";
return 0;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
```

The output of above program is

```
Enter a decimal number: 10
Step 1: 10/2, Remainder = 0, Quotient = 5
Step 2: 5/2, Remainder = 1, Quotient = 2
Step 3: 2/2, Remainder = 0, Quotient = 1
Step 4: 1/2, Remainder = 1, Quotient = 0
10 in decimal = 1010 in binary
```

In the above example, We have created one function `convertBinaryToDecimal()` which converts the decimal number to the binary number.

Subscribe

Login

0 Comments

Inline Feedbacks

View all comments