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:

88 -> 130
78 -> 116

The steps to convert the decimal number to octal number are as follows:

  1. Take Octal = 0, iterator = 1.
  2. Store the remainder when the decimal number is divided by 8.
  3. Divide the decimal number by 8.
  4. Octal = Octal + iterator * remainder.
  5. Multiply the iterator by 10.
  6. Repeat the steps from 2 to 5 until decimal number is not 0.
  7. Print the Octal Number. 

C++ Program to convert Decimal to Octal Number is as follows:

/* C++ Program to Convert Decimal Number to Octal Number */  
#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:

  1. Program to convert decimal number to binary number.
  2. Program to convert decimal number to hexadecimal number.
  3. Program to convert hexadecimal number to binary number.
  4. Program to convert hexadecimal number to octal number.
  5. Program to convert hexadecimal number to decimal number.
  6. Program to check Leap Year.
  7. Program to convert binary number to octal number.
  8. Program to convert binary number to decimal number.
  9. Program to convert binary number to hexadecimal number.
  10. Program to convert octal number to binary number.
  11. Program to convert octal number to decimal number.
  12. Program to convert octal number to hexadecimal number.
  13. Program to find largest amongst three numbers.
  14. Program to find factorial of a number.
  15. Program to find GCD of two numbers.
  16. Program to find LCM of two numbers.
  17. Program to check whether entered number is odd or even.
  18. Program to check whether entered number is prime number or not.
  19. Program to check whether entered number is palindrome or not.
  20. Program to check whether entered number is Armstrong Number or Not.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *