Decimal to Octal Conversion
“Decimal to Octal Conversion” is one of the classic programming problem exercise. Here, we are given a decimal number, entered by user and our task is to convert the given decimal number to its equivalent octal number.
Example (Decimal to Octal Conversion):
88 -> 130 78 -> 116
The steps for decimal to octal conversion are as follows:
- Take Octal = 0, iterator = 1.
- Store the remainder when the decimal number is divided by 8.
- Divide the decimal number by 8.
- Octal = Octal + iterator * remainder.
- Multiply the iterator by 10.
- Repeat the steps from 2 to 5 until decimal number is not 0.
- Print the Octal Number.
C++ Program for Decimal to Octal Conversion is as follows:
/* C++ Program for Decimal to Octal Conversion */
#include<bits/stdc++.h>
using namespace std;
int main()
{
/* Scan the Decimal Number */
int Decimal;
cout<<"Enter the Decimal Number: ";
cin>>Decimal;
/* Converting Decimal to Octal */
int Octal = 0;
int iterator = 1;
int temp = Decimal;
while(temp != 0)
{
int remainder = temp % 8;
temp = temp / 8;
Octal = Octal + remainder*iterator;
iterator = iterator * 10;
}
/*Printing the result */
cout<<"The Octal of "<<Decimal<<" is "<<Octal;
}
OUTPUT: Enter the Decimal Number: 78 The Octal of 78 is 116
Related Posts:
- Program to convert decimal number to binary number.
- Program to convert decimal number to hexadecimal number.
- Program to convert hexadecimal number to binary number.
- Program to convert hexadecimal number to octal number.
- Program to convert hexadecimal number to decimal number.
- Program to convert binary number to octal number.
- Program to convert binary number to decimal number.
- Program to convert binary number to hexadecimal number.
- Program to convert octal number to binary number.
- Program to convert octal number to decimal number.
- Program to convert octal number to hexadecimal number.
- Program to print Fibonacci Series up to Nth Term.
- Program to find and print nth Fibonacci number.
- Program to find Power of the Number.
- Program to find Quotient and Remainder.
- Program to find largest amongst three numbers.
- Program to find factorial of a number.
- Program to find GCD of two numbers.
- Program to find LCM of two numbers.
- Program to check whether entered number is odd or even.