Program for decimal to octal conversion
Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent octal number. i.e convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, octal number system uses 8 digits from 0-7 and decimal number system uses 10 digits 0-9 to represent any numeric value.
Examples:
Input : 16 Output : 20 Input : 10 Output : 12 Input: 33 Output: 41
Algorithm:
- Store the remainder when the number is divided by 8 in an array.
- Divide the number by 8 now
- Repeat the above two steps until the number is not equal to 0.
- Print the array in reverse order now.
For Example:
If the given decimal number is 16.
Step 1: Remainder when 16 is divided by 8 is 0. Therefore, arr[0] = 0.
Step 2: Divide 16 by 8. New number is 16/8 = 2.
Step 3: Remainder when 2 is divided by 8 is 2. Therefore, arr[1] = 2.
Step 4: Divide 2 by 8. New number is 2/8 = 0.
Step 5: Since number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore the equivalent octal number is 20.
Below diagram shows an example of converting the decimal number 33 to equivalent octal number.
Below is the implementation of above idea.
C/C++
// C++ program to convert a decimal // number to octal number #include <iostream> using namespace std; // function to convert decimal to octal void decToOctal( int n) { // array to store octal number int octalNum[100]; // counter for octal number array int i = 0; while (n != 0) { // storing remainder in octal array octalNum[i] = n % 8; n = n / 8; i++; } // printing octal number array in reverse order for ( int j = i - 1; j >= 0; j--) cout << octalNum[j]; } // Driver program to test above function int main() { int n = 33; decToOctal(n); return 0; } |
Java
// Java program to convert a decimal // number to octal number import java.io.*; class GFG { // Function to convert decimal to octal static void decToOctal( int n) { // array to store octal number int [] octalNum = new int [ 100 ]; // counter for octal number array int i = 0 ; while (n != 0 ) { // storing remainder in octal array octalNum[i] = n % 8 ; n = n / 8 ; i++; } // Printing octal number array in reverse order for ( int j = i - 1 ; j >= 0 ; j--) System.out.print(octalNum[j]); } // driver program public static void main (String[] args) { int n = 33 ; decToOctal(n); } } // Contributed by Pramod Kumar |
Python3
# Python3 program to convert # a decimal number to # octal number # function to convert # decimal to octal def decToOctal(n): # array to store # octal number octalNum = [ 0 ] * 100 ; # counter for octal # number array i = 0 ; while (n ! = 0 ): # storing remainder # in octal array octalNum[i] = n % 8 ; n = int (n / 8 ); i + = 1 ; # printing octal number # array in reverse order for j in range (i - 1 , - 1 , - 1 ): print (octalNum[j], end = ""); # Driver Code n = 33 ; decToOctal(n); # This code is contributed # by mits |
C#
// C# program to convert a decimal // number to octal number using System; class GFG { // Function to convert decimal to octal static void decToOctal( int n) { // array to store octal number int []octalNum = new int [100]; // counter for octal number array int i = 0; while (n != 0) { // storing remainder in octal array octalNum[i] = n % 8; n = n / 8; i++; } // Printing octal number array in // reverse order for ( int j = i - 1; j >= 0; j--) Console.Write(octalNum[j]); } // driver program public static void Main () { int n = 33; decToOctal(n); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP program to convert // a decimal number to // octal number // function to convert // decimal to octal function decToOctal( $n ) { // array to store // octal number $octalNum ; // counter for octal // number array $i = 0; while ( $n != 0) { // storing remainder // in octal array $octalNum [ $i ] = $n % 8; $n = (int)( $n / 8); $i ++; } // printing octal number // array in reverse order for ( $j = $i - 1; $j >= 0; $j --) echo $octalNum [ $j ]; } // Driver Code $n = 33; decToOctal( $n ); // This code is contributed // by ajit ?> |
Output:
41
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Program for octal to decimal conversion
- Decimal to octal conversion with minimum use of arithmetic operators
- Program for Decimal to Binary Conversion
- Program for Binary To Decimal Conversion
- Check if Decimal representation of an Octal number is divisible by 7
- Decimal to binary conversion without using arithmetic operators
- Program to Convert Hexadecimal to Octal
- Program to Convert Octal Number to Binary Number
- Program for hexadecimal to decimal
- Recursive Program for Binary to Decimal
- Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation
- Python program to covert decimal to binary number
- Convert a binary number to octal
- Type Conversion in Python
- Flip-flop types and their Conversion