C code for decimal to octal converter


Levels of difficulty: / perform operation:

Octal number system: It is base 8 number system which uses the digits from 0 to 7.

Decimal number system:
It is base 10 number system which uses the digits from 0 to 9

Decimal to octal conversion method:

Step 1: Divide the original decimal number by 8
Step 2: Divide the quotient by 8
Step3: Repeat the step 2 until we get quotient equal to zero.

Result octal number would be remainders of each step in the reverse order.

Decimal to octal conversion with example:

For example we want to convert decimal number 525 in the octal.

Step 1: 525 / 8 Remainder : 5 , Quotient : 65
Step 2: 65 / 8 Remainder : 1 , Quotient : 8
Step 3: 8 / 8 Remainder : 0 , Quotient : 1
Step 4: 1 / 8 Remainder : 1 , Quotient : 0

So equivalent octal number is: 1015
That is (525)10 = (1015)8





C Program 1

#include<stdio.h>
int main() {
	long int decimalNumber,remainder,quotient;
	int octalNumber[100],i=1,j;
	printf("Enter any decimal number: ");
	scanf("%ld",&decimalNumber);
	quotient = decimalNumber;
	while(quotient!=0) {
		octalNumber[i++]= quotient % 8;
		quotient = quotient / 8;
	}
	printf("Equivalent octal value of decimal number %d: ",decimalNumber);
	for (j = i -1 ;j> 0;j--)
	      printf("%d",octalNumber[j]);
	return 0;
}

Output 1

Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62





C Program 2

#include<stdio.h>
int main() {
	long int decimalNumber;
	printf("Enter any decimal number : ");
	scanf("%d",&decimalNumber);
	printf("Equivalent octal number is: %o",decimalNumber);
	return 0;
}

Output 2

Enter any decimal number: 25
Equivalent octal number is: 31