- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program to Convert Octal to Decimal
In this article, you will learn and get code for octal to decimal conversion in C++. The octal to decimal conversion program is designed both with and without functions.
But before starting the program, if you're not aware of the simple formula and steps used for the conversion, you can refer to the octal to decimal formula to get all the required information.
Octal to Decimal in C++
To convert an octal number to a decimal number in C++ programming, you have to ask the user to enter the octal number and then convert it into its equivalent decimal value. Print the equivalent decimal value on the output as shown in the program given below.
The question is: write a program in C++ that converts an octal number to a decimal number. Here is its answer:
#include<iostream> #include<math.h> using namespace std; int main() { int octalNum, decimalNum=0, i=0, rem; cout<<"Enter the Octal Number: "; cin>>octalNum; while(octalNum!=0) { rem = octalNum%10; decimalNum = decimalNum + (rem*pow(8,i)); i++; octalNum = octalNum/10; } cout<<"\nEquivalent Decimal Value: "<<decimalNum; cout<<endl; return 0; }
This program was built and runs under the Code::Blocks IDE. Here is its sample run:
Now supply the input as any octal number, say 345, and press the ENTER key to convert and print its equivalent decimal value as shown here in the snapshot of sample output given below:
The dry run of the above program with octal input 345 goes like this:
- Initial values are as follows: decimalNum=0, i=0.
- And when the user enters an octal number as input, say 345, then it gets stored in the octalNum variable. So octalNum=345
- Now the condition octalNum!=0 or 345!=0 evaluates to be true, therefore program flow goes inside the while loop.
- There, octalNum%10, 345%10, or 5 is set to rem. So rem=5
- And decimalNum + (rem*pow(8,i)) or 0+(5*8i) or 0+(5*80) or 5*1 or 5 gets initialized to decimalNum. So decimalNum=5
- The value of i gets incremented. So i=1
- And octalNum/10, 345/10, or 34 is set to octalNum. So octalNum=34
- Program flow goes back and evaluates the condition of the while loop again.
- The execution of the while continues until its condition evaluates to be false.
- Before its condition evaluates to be false, we'll get values (after each evaluation) as follows:
- rem=5, decimalNum=5, i=1, octalNum=34
- rem=4, decimalNum=37, i=2, octalNum=3
- rem=3, decimalNum=229, i=3, octalNum=0
- Now print the value of decimalNum (229), which will be the decimal equivalent of the given octal number (345).
Octal to decimal conversion in C++ without using the pow() function
To create the same program as the previous one without using the pow() function, just replace the following statement:
decimalNum = decimalNum + (rem*pow(8,i)); i++;
with the statement given below:
decimalNum = decimalNum + (rem*mul); mul = mul*8;
Note: Don't forget to declare the variable mul at the start of the program. and initialize its initial value as 1.
Remove the declaration and initialization of variable i from the program. Also remove the header file, math.h. Everything else will remain the same.
Program to deal with invalid octal input
The octal number has an 8 as its base. Therefore, it has a total of eight digits, which are from 0 to 7. Other digits, like 8 and 9, are not valid octal digits. Therefore, if the user enters a number that contains 8 or 9, Then here is the program to deal with:
#include<iostream> using namespace std; int main() { int octalNum, rem, temp, chk=0; cout<<"Enter the Octal Number: "; cin>>octalNum; temp = octalNum; while(temp!=0) { rem = temp%10; if(rem>=8) { chk++; break; } temp = temp/10; } if(chk==0) { int decimalNum=0, mul=1; while(octalNum!=0) { rem = octalNum%10; decimalNum = decimalNum + (rem*mul); mul = mul*8; octalNum = octalNum/10; } cout<<"\nEquivalent Decimal Value: "<<decimalNum; } else cout<<"\nInvalid Octal Digit!"; cout<<endl; return 0; }
Here is a sample run with 349 as an octal number as user input. Because it contains 9 (the third digit of 349) and 9 is an invalid octal digit, here is the output you will see:
Using a Function to Convert Octal to Decimal in C++
This is the last program on octal to decimal conversion in C++. It is created using a user-defined function, octalToDecimal(). The function takes an octal number as its argument and returns either its equivalent decimal value or 0.
It returns 0 if there is an invalid octal digit in the octal number that was entered. Otherwise, it returns the decimal value that the octal number would be in decimal.
#include<iostream> using namespace std; int OctalToDecimal(int); int main() { int octalNum, decimalNum; cout<<"Enter the Octal Number: "; cin>>octalNum; decimalNum = OctalToDecimal(octalNum); if(decimalNum==0) cout<<"\nInvalid Octal Digit!"; else cout<<"\nEquivalent Decimal Value: "<<decimalNum; cout<<endl; return 0; } int OctalToDecimal(int octalNum) { int temp, rem, chk=0; temp = octalNum; while(temp!=0) { rem = temp%10; if(rem>=8) { chk++; break; } temp = temp/10; } if(chk==0) { int decimalNum=0, mul=1; while(octalNum!=0) { rem = octalNum%10; decimalNum = decimalNum + (rem*mul); mul = mul*8; octalNum = octalNum/10; } return decimalNum; } else return 0; }
This program produces the same output as the previous program.
The same program in different languages
« Previous Program Next Program »