Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ Program For Hexadecimal To Decimal Conversion
Next article icon

C++ Program For Decimal To Octal Conversion

Last Updated : 03 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The octal numbers are a base 8 number system that uses digits from 0-7 and the decimal numbers are a base 10 numbers system that uses 10 digits from 0-9 to represent any numeric value.

In this article, we will learn how to write a C++ program to convert a given decimal number into an equivalent octal number. i.e. convert the number with base value 10 to base value 8.

Algorithm to Convert Decimal Numbers to Octal in C++

  1. Create an array to store the octal representation.
  2. Run a loop till the number is not zero.
  3. Extract the remainder by taking the mod of the number by 8 and store the remainder in the array as an octal digit.
  4. Update the number by dividing it by 8 in each iteration.
  5. Print the array in reverse order.

The below diagram shows an example of converting the decimal number 33 to an equivalent octal number.  

decimal to octal conversion in C++

C++ Program to Convert Decimal Number To Octal Number

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 Code int main() {     int n = 33;      // Function Call     decToOctal(n);      return 0; } 

Output
41

Explanation

If the given decimal number is 33.

  • Step 1: Remainder when 33 is divided by 8 is 1. Therefore, arr[0] = 1. 
  • Step 2: Divide 33 by 8. The new number is 33/8 = 4. 
  • Step 3: Remainder, when 4 is divided by 8, is 4. Therefore, arr[1] = 4. 
  • Step 4: Divide 4 by 8. The new number is 4/8 = 0. 
  • Step 5: Since the number becomes = 0.

Stop repeating steps and print the array in reverse order. Therefore, the equivalent octal number is 41.

Complexity Analysis

  • Time Complexity: O(log N)
  • Space Complexity: O(N), since creating an array to store octal numbers.

Refer to the complete article Program for Decimal to Octal Conversion for more methods to convert Decimal numbers to Octal numbers.


Next Article
C++ Program For Hexadecimal To Decimal Conversion
author
kartik
Improve
Article Tags :
  • C++ Programs
  • C++
  • C++ Conversion Programs
Practice Tags :
  • CPP

Similar Reads

  • C++ Program For Octal To Decimal Conversion
    Given an octal number as input, we need to write a program to convert the given octal number into an equivalent decimal number. Examples: Input : 67Output: 55 Input : 512Output: 330 Input : 123Output: 83 1. Simple ApproachThe idea is to extract the digits of a given octal number starting from the ri
    2 min read
  • C++ Program For Hexadecimal To Decimal Conversion
    The hexadecimal numbers are base 16 numbers that use 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all digits. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15). Decimal numbers are base 10 numbers with 10 symbols to represent all digits. In this article, we will l
    3 min read
  • C++ Program For Decimal To Hexadecimal Conversion
    In this article, we will learn to write a C++ program to convert a decimal number into an equivalent hexadecimal number. i.e. convert the number with base value 10 to base value 16. In the decimal system, we use ten digits (0 to 9) to represent a number, while in the hexadecimal system, we use sixte
    3 min read
  • C++ Program For Binary To Decimal Conversion
    The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++. Algorithm to Convert Binary Numbers to DecimalInitialize a v
    4 min read
  • C++ Program For Decimal To Binary Conversion
    Binary Numbers uses only 0 and 1 (base-2), while Decimal Number uses 0 to 9 (base-10). In this article, we will learn to implement a C++ program to convert Decimal numbers to Binary Numbers. The below diagram shows an example of converting the decimal number 17 to an equivalent binary number. Recomm
    3 min read
  • C++ Program For Binary To Octal Conversion
    The problem is to convert the given binary number (represented as a string) to its equivalent octal number. The input could be very large and may not fit even into an unsigned long long int. Examples: Input: 110001110Output: 616 Input: 1111001010010100001.010110110011011Output: 1712241.26633 Simple
    5 min read
  • C++ Program For char to int Conversion
    In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. In this article, we will learn how to convert char to int in C++. Example: Inp
    2 min read
  • C++ Program For int to char Conversion
    In this article, we will learn how to convert int to char in C++. For this conversion, there are 5 ways as follows: Using typecasting.Using static_cast.Using sprintf().Using to_string() and c_str().Using stringstream. Let's start by discussing each of these methods in detail. Examples: Input: N = 65
    6 min read
  • C++ Program For Double to String Conversion
    Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76
    2 min read
  • C++ Program For String to Double Conversion
    There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences