C program to convert Decimal to Octal number system

Write a C program to input decimal number from user and convert to octal number system. How to convert from decimal number system to octal number system in C programming. Logic to convert decimal to octal number system in C programming.

Example

Input

Input decimal: 22

Output

Octal number: 26

Required knowledge

Basic C programming, While loop

Decimal number system

Decimal number system is a base 10 number system. Decimal number system uses 10 symbols to represent all numbers i.e. 0123456789.

Octal number system

Octal number system is a base 8 number system. Octal number system uses 8 symbols to represent all numbers i.e. 01234567

Algorithm to convert decimal to octal

Algorithm Decimal to Octal conversion
    begin:
        read(decimal);
        octal ← 0; place ← 1; rem ← 0;
        While (decimal > 0) do
            begin:
                remdecimal % 8;
                octal ← (rem * place) + octal;
                placeplace * 10;
                decimaldecimal / 8;
            end;
        print('Octal number' octal);
    end;

Program to convert decimal to octal number system

/**
 * C program to convert from Decimal to Octal number system
 */

#include <stdio.h>

int main()
{
    long long decimal, tempDecimal, octal;
    int i, rem, place = 1;

    octal = 0;

    /* Input decimal number from user */
    printf("Enter any decimal number: ");
    scanf("%lld", &decimal);

    tempDecimal = decimal;

    /* Decimal to octal conversion */
    while(tempDecimal > 0)
    {
        rem = tempDecimal % 8;

        octal = (rem * place) + octal;

        tempDecimal /= 8;

        place *= 10;
    }

    printf("\nDecimal number = %lld\n", decimal);
    printf("Octal number = %lld", octal);

    return 0;
}

Output

Enter any decimal number: 20
Decimal number = 20
Octal number = 24

Happy coding 😉