C Program to Convert Decimal to Octal Number

C program to convert decimal to Octal; In this tutorial, i am going to show you how to convert decimal numbers to octal numbers in c program with the help of for loop, while loop and function.

All C Programs to Convert Decimal to Octal Number

Let’s see all c programs to convert decimal to octal number; is as follow:

  • 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 result 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 result 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 result of the above c program; as follows:

 Please Enter the Number You want to Convert  :  22

 Equivalent Octal Number of 22 =  26

Be First to Comment

Leave a Reply

Your email address will not be published.