This is a C program to Convert Decimal to Octal.
This program takes a decimal number as input and converts to octal number.
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 the 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.
Here is source code of the C program to Convert Decimal to Octal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to Convert Decimal to Octal
*/
#include <stdio.h>
int main()
{
long decimalnum, remainder, quotient;
int octalNumber[100], i = 1, j;
printf("Enter the decimal number: ");
scanf("%ld", &decimalnum);
quotient = decimalnum;
while (quotient != 0)
{
octalNumber[i++] = quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent octal value of decimal no %d: ", decimalnum);
for (j = i - 1; j > 0; j--)
printf("%d", octalNumber[j]);
return 0;
}
1. Take a decimal number as input and store it in the variable decimalnum.
2. Copy the variable decimalnum to the variable quotient.
3. Divide the variable quotient and obtain its remainder and quotient. Store the remainder in the array octalNumber and override the variable quotient with the quotient obtained.
4. Repeat the step 3 until the quotient becomes zero.
5. When it becomes zero, print the array octalNumber in the reverse order to get the output.
Output: Enter the decimal number: 68 Equivalent octal value of decimal no 68: 104
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms