Convert Decimal number to Octal number

Decimal number to Octal number

Program to convert Decimal number to Octal number

So, First of all understand the procedure of the program that how to convert Decimal number to Octal number.

Procedure

  1. Take a decimal number as input.
  2. Divide the input number by 8 and obtain its remainder and quotient. Store the remainder in the array.
  3. Repeat step 2 with the quotient obtained. Do this until the quotient becomes zero.
  4. Print the array in the reverse order to get the output.
#include <stdio.h>
#include<conio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);
void main()
{
    int decimalNumber;
printf("\n\t\t\t...Welcome To EduNews.Tech... ");

    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);

    printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));

    //getch();
}

int convertDecimalToOctal(int decimalNumber)
{
    int octalNumber = 0, i = 1;

    while (decimalNumber != 0)
    {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}

Program Output:

decimal to octal

I hope this post helps you to understand the program that how to “Convert Decimal number to Octal number” and its implementation in C programming language.

Keep coding 🙂

Leave a Reply

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