C Program to Convert Decimal to Octal Number

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

  • 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.