Course
Introduction
C++ "Hello, World!" ProgramPrint Number Entered by UserAdd Two NumbersFind Quotient and RemainderFind Size of int, float, double and char in Your SystemSwap Two NumbersFind ASCII Value of a CharacterMultiply two NumbersDecisions and Loops
Check Whether Number is Even or OddCheck Whether a character is Vowel or ConsonantFind Largest Number Among Three NumbersFind All Roots of a Quadratic EquationCalculate Sum of Natural NumbersCheck Leap YearFind FactorialGenerate Multiplication TableDisplay Fibonacci SeriesFind GCDFind LCMReverse a NumberCalculate Power of a NumberCheck Whether a Number is Palindrome or NotCheck Whether a Number is Prime or NotDisplay Prime Numbers Between Two IntervalsCheck Armstrong NumberDisplay Armstrong Number Between Two IntervalsDisplay Factors of a NumberCreate Pyramid and PatternMake a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...caseFunctions
Display Prime Numbers Between Two Intervals Using FunctionsCheck Prime Number By Creating a FunctionCheck Whether a Number can be Express as Sum of Two Prime NumbersFind Sum of Natural Numbers using RecursionCalculate Factorial of a Number Using RecursionFind G.C.D Using RecursionConvert Binary Number to Decimal and vice-versaConvert Octal Number to Decimal and vice-versaConvert Binary Number to Octal and vice-versaReverse a Sentence Using RecursionCalculate Power Using RecursionArrays and Strings
Calculate Average of Numbers Using ArraysFind Largest Element of an ArrayCalculate Standard DeviationAdd Two Matrix Using Multi-dimensional ArraysMultiply Two Matrix Using Multi-dimensional ArraysFind Transpose of a MatrixMultiply two Matrices by Passing Matrix to FunctionAccess Elements of an Array Using PointerSwap Numbers in Cyclic Order Using Call by ReferenceFind the Frequency of Characters in a StringFind the Number of Vowels, Consonants, Digits and White Spaces in a StringRemove all Characters in a String Except AlphabetsFind the Length of a StringConcatenate Two StringsCopy StringsSort Elements in Lexicographical Order (Dictionary Order)C++ Program to Convert Binary Number to Octal and vice-versa
To understand this example, you should have the knowledge of the following C++ programming topics:
- C++ Functions
- C++ User-defined Function Types
- C++ if, if…else and Nested if…else
- C++ while and do…while Loop
Example 1: Program to Convert Binary to Octal
In this program, we will first convert the binary number to decimal. Then, the decimal number is converted to octal.
#include <iostream>
#include <cmath>
using namespace std;
int convertBinarytoOctal(long long);
int main()
{
long long binaryNumber;
cout << "Enter a binary number: ";
cin >> binaryNumber;
cout << binaryNumber << " in binary = " << convertBinarytoOctal(binaryNumber) << " in octal ";
return 0;
}
int convertBinarytoOctal(long long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;
while(binaryNumber != 0)
{
decimalNumber += (binaryNumber%10) * pow(2,i);
++i;
binaryNumber/=10;
}
i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Output
Enter a binary number: 10001
10001 in binary = 21 in octal
The binary number entered by the user is passed to convertBinaryToOctal()
function. And, this function converts the number to octal and returns to the main()
function
Example 2: Program to Convert Octal to Binary
In this program, the octal number is converted to decimal at first. Then, the decimal number is converted to binary number.
#include <iostream>
#include <cmath>
using namespace std;
long long convertOctalToBinary(int);
int main()
{
int octalNumber;
cout << "Enter an octal number: ";
cin >> octalNumber;
cout << octalNumber << " in octal = " << convertOctalToBinary(octalNumber) << "in binary";
return 0;
}
long long convertOctalToBinary(int octalNumber)
{
int decimalNumber = 0, i = 0;
long long binaryNumber = 0;
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;
while (decimalNumber != 0)
{
binaryNumber += (decimalNumber % 2) * i;
decimalNumber /= 2;
i *= 10;
}
return binaryNumber;
}
Output
Enter an octal number: 54
54 in octal = 101100
The octal number entered by the user is passed to convertOctalToBinary()
function. And, this function converts the number to binary and returns the main()
function