Loop Assignment 16 — Convert a Decimal Number to Octal Using a Do-While Loop
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Convert a Decimal Number to Octal Using a Do-While Loop — Repeated Division by 8

📑 On this page:
  • How Decimal to Octal Conversion Works
  • The Three Steps of Conversion
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How to convert a decimal number into its octal equivalent
  • How to use a do-while loop for repeated division
  • How remainders become octal digits
  • How to build the final octal number using place values
  • Why the do-while loop is suitable for handling input 0
  • How to validate negative decimal input

How Decimal to Octal Conversion Works

Octal is a base-8 number system. It uses only eight digits: 0, 1, 2, 3, 4, 5, 6, and 7. To convert a decimal number into octal, divide the decimal number repeatedly by 8 and collect the remainders.

The remainder obtained in each division is one octal digit. However, the remainders are produced from the last digit to the first digit. Therefore, they must be arranged in reverse order to get the correct octal number.

In this program, instead of storing remainders in an array, we build the octal number directly using place values. The first remainder becomes the unit digit, the second remainder becomes the eights digit, the third remainder becomes the sixty-fours digit, and so on.

💡 In one line: Repeatedly divide the decimal number by 8, use each remainder as an octal digit, and build the answer from right to left using place values.

Diagram showing decimal to octal conversion using repeated division by 8 and a do-while loop

The Three Steps of Conversion

Step Operation Effect
1. Find the remainderremainder = decimal % 8;Gives the current octal digit
2. Build the octal numberoctal = remainder * place + octal;Places the digit at the correct position
3. Update the number and placedecimal /= 8; and place *= 10;Prepares the next division and next digit position

The do-while loop is appropriate here because it executes its body at least once. This is useful when the user enters 0, because the program should still display octal output 0.

For example, to convert decimal 25 to octal, divide 25 by 8. The remainder is 1 and the quotient is 3. Then divide 3 by 8. The remainder is 3 and the quotient becomes 0. Reading the remainders in reverse order gives 31, so decimal 25 is octal 31.

Complete C Program

Program: convert a decimal number to octal using a do-while loop

#include <stdio.h>

int main(void) {
    int decimal, remainder, place = 1, octal = 0;

    printf("Enter a non-negative decimal number: ");
    if (scanf("%d", &decimal) != 1 || decimal < 0) {
        printf("Invalid input. Please enter a non-negative integer.\n");
        return 1;
    }

    do {
        remainder = decimal % 8;             /* current octal digit */
        octal = remainder * place + octal;   /* build octal number */
        decimal = decimal / 8;               /* remove processed digit */
        place = place * 10;                  /* move to next place value */
    } while (decimal != 0);

    printf("\nOctal equivalent: %d\n", octal);

    return 0;
}
                                    

Program Output

Enter a non-negative decimal number: 25

Octal equivalent: 31
                                

A second run, with decimal 0:

Enter a non-negative decimal number: 0

Octal equivalent: 0
                                

In the first run, decimal 25 is converted to octal 31. In the second run, the do-while loop executes once even though the input is 0, so the program correctly prints octal 0.

Step-by-Step Explanation

Let us understand how the program converts decimal 25 into octal.

1. Initial values

The input decimal number is 25. The variables are initialized as octal = 0 and place = 1. The place variable decides whether the current remainder should be placed in the units, tens, hundreds, or another position.

2. First loop iteration

remainder = 25 % 8 gives 1. Then octal = 1 * 1 + 0, so octal becomes 1. After that, decimal = 25 / 8 becomes 3, and place becomes 10.

3. Second loop iteration

remainder = 3 % 8 gives 3. Then octal = 3 * 10 + 1, so octal becomes 31. After that, decimal = 3 / 8 becomes 0, and place becomes 100.

4. Loop termination

The loop condition is decimal != 0. Since decimal has become 0, the loop stops. The final value of octal is 31.

5. Why do-while is used

A do-while loop checks its condition after executing the loop body. Therefore, even if the user enters 0, the loop runs once and produces octal 0. A normal while loop would skip the body completely when the input is 0.

6. Why place is multiplied by 10

The variable place stores the positional value of the next octal digit. It starts at 1 for the units position, becomes 10 for the next position, then 100, and so on. This allows the program to build the octal number without using an array.

7. Why negative input is rejected

This program is designed for non-negative decimal numbers. Negative numbers require a separate sign-handling approach, so the program validates the input and displays an error message if the number is negative.

8. Octal digits are always 0 to 7

When any decimal number is divided by 8, the remainder can only be 0, 1, 2, 3, 4, 5, 6, or 7. Therefore, every remainder produced by decimal % 8 is a valid octal digit.

9. Decimal to octal conversion table

Decimal Division Steps Octal
88 ÷ 8 = 1 remainder 0; 1 ÷ 8 = 0 remainder 110
2525 ÷ 8 = 3 remainder 1; 3 ÷ 8 = 0 remainder 331
6464 ÷ 8 = 8 remainder 0; 8 ÷ 8 = 1 remainder 0; 1 ÷ 8 = 0 remainder 1100
100100 ÷ 8 = 12 remainder 4; 12 ÷ 8 = 1 remainder 4; 1 ÷ 8 = 0 remainder 1144

10. Complexity summary

Time complexity is O(log8 n), because the decimal number is divided by 8 in every iteration. Space complexity is O(1), because the program uses only a few integer variables and does not store the octal digits in an array.

🎯 Remember this: To convert decimal to octal, repeatedly calculate decimal % 8, build the result using place values, and divide the number by 8. Use a do-while loop so input 0 is also processed correctly.

Frequently Asked Questions

1. What is the octal number system?

The octal number system is a base-8 number system. It uses the digits 0 through 7. Each octal digit represents three binary digits.

2. Why do we divide by 8 for octal conversion?

We divide by 8 because octal has base 8. The remainder after division by 8 gives the current octal digit, and the quotient is used for the next division.

3. Why are remainders reversed?

Repeated division produces octal digits from right to left. The first remainder is the last digit of the octal number, so the remainders must be read in reverse order.

4. Why is a do-while loop better than a while loop here?

A do-while loop executes at least once. This is useful when the input is 0, because the program must still display octal 0. A while loop would skip the loop body if the condition is false initially.

5. Can this program handle negative decimal numbers?

This version rejects negative input. To support negative numbers, you would need to store and print a minus sign, then convert the absolute value of the number to octal.

6. What is the time complexity of this program?

The time complexity is O(log8 n), because the decimal number is divided by 8 in every iteration. The space complexity is O(1), since no array is used to store the octal digits.

💡 Tip: After implementing this program, try converting decimal to binary using the same logic. Instead of dividing by 8, divide by 2 and use base 2 for the place values.

📖 Related Tutorials

  • 17. Count the Number of 1s in the Binary Representation of an Integer Using a While Loop
  • 18. Convert a Hexadecimal String to Decimal Using a For Loop
  • 15. Find the Sum of All Digits of a Given Number
  • All Loop Assignments in C

Previous Topic: -->> 20. Write a program in C to calculate the factorial of a given number using a for loop.   ||   Next Topic: -->> 17. Write a program in C to count the number of 1s in the binary representation of an integer using a while loop.


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.