C Program to Convert Decimal to Octal Number

C program to convert decimal to Octal; Through this tutorial, we will learn how to convert decimal numbers to octal numbers in c program using for loop, while loop and function.

Programs to Convert Decimal to Octal Number in C

Use the following programs to convert decimal numbers to octal numbers in c program using for loop, while loop and function in c:

  • C Program to Convert Decimal to Octal Number using While Loop
  • C Program to Convert Decimal to Octal Number using For Loop
  • C Program to Convert Decimal to Octal Number using Function

C Program to Convert Decimal to Octal Number using While Loop

/* C Program to Convert Decimal to Octal Number */

#include <stdio.h>

int main()
{
    int octalNumber[10], number, i, j;
    printf("\n Please Enter Decimal Number  :  ");
    scanf("%d", &number);

    while(number != 0)
    {
        octalNumber[i++] = number % 8;
        number = number / 8;
    }

    printf("\n Equivalent Octal Number of a Given Number =  ");
    for(j = i - 1; j > 0; j--)
    {
        printf("%d", octalNumber[j]);
    }
    return 0;
}

The output of the above c program; as follows:

Please Enter Decimal Number  :  110110
Equivalent Octal Number of a Given Number =  327036

C Program to Convert Decimal to Octal Number using For Loop

/* C Program to Convert Decimal to Octal Number */

#include <stdio.h>
int main()
{
    int octalNumber[10], number, i, j;
    printf("\n Please Enter Decimal Number  :  ");
    scanf("%d", &number);

    for(i = 0; number > 0; i++)
    {
        octalNumber[i] = number % 8;
        number = number / 8;
    }

    printf("\n Equivalent Octal Number of a Given Number =  ");
    for(j = i - 1; j >= 0; j--)
    {
        printf("%d", octalNumber[j]);
    }
    return 0;
}

The output of the above c program; as follows:

Please Enter Decimal Number  :  1420
Equivalent Octal Number of a Given Number =  2614

C Program to Convert Decimal to Octal Number using Function

/* C Program to Convert Decimal to Octal Number */

#include <stdio.h>

long Decimal_to_Octal(int number)
{
    long octalNumber = 0, i = 1;
    int remainder;
    while(number != 0)
    {
        remainder = number % 8;
        number = number / 8;
        octalNumber = octalNumber + (remainder * i);
        i = i * 10;
    }
    return octalNumber;
}

int main()
{
    int number;
    printf("\n Please Enter Decimal Number  :  ");
    scanf("%d", &number);

    long oct  = Decimal_to_Octal(number);
    printf("\n Equivalent Octal Number of %d =  %ld", number, oct);

    return 0;
}

The output of the above c program; as follows:

 Please Enter the Number You want to Convert  :  22

 Equivalent Octal Number of 22 =  26

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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