In this example, you will learn to write the C program to decimal to octal numbers. Here we write a program that takes a decimal number as input and converts it into an equivalent octal number. Converting a decimal number to an octal means converting the number with base value 10 to base value 8.
The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, the octal number system uses 8 digits from 0-7 and the decimal number system uses 10 digits 0-9 to represent any numeric value.
Examples:
Decimal number Input: 10 Octal number Output: 12 Decimal number Input: 9 Octal number Output: 11
Logic to convert decimal to octal number:
1. Ask the user to enter the decimal number.
2. Now perform modulo division on the number by 8 and store the remainder in an array.
//Modulo division decimal number by 8 octalNum[i] = num % 8;
3. Now Divide the number by 8.
//Divide the decimal number by 8 num = num / 8;
4. Repeat steps 2 and 3 until the number is greater than zero.
5. In the last we will print the array in reverse order.
Example 1: C Program to Convert Decimal to Octal
#include <stdio.h> #define CHAR_SIZE 8 #define ARRAY_SIZE sizeof(int)* CHAR_SIZE int main() { int i = 0,j =0; //num for decimal number long long num = 0; //Array to store octal number int octalNum[ARRAY_SIZE]; printf("Enter decimal number: "); scanf("%lld", &num); while (num > 0) { octalNum[i] = (num % 8); num = (num / 8); i++; } // printing octal array in reverse order for (j = i - 1; j >= 0; j--) { printf("%d",octalNum[j]); } return 0; }
Output:
Enter a decimal number: 10
12
Code Analysis,
Suppose num is 10.
Step 1: Remainder, when 10 is divided by 8, is 2. Therefore, octalNum[0] = 2.
Step 2: Divide 10 by 8. The new number is 10/8= 1.
Step 3: Remainder, when 1 is divided by 8, is 1. Therefore, binaryNum[1] = 1.
Step 4: Divide 1 by 8. New number is 1/8 = 0.
Step 5: Since the number becomes = 0. Print the octal array in reverse order. Therefore the equivalent binary number is 12.
Example 2: Second way to convert decimal to octal without an array:
We can also convert decimal to octal numbers without using an array. So let’s see the approach,
1. Initialize the variables octalNum to 0 and countVal to 1.
2. Ask the user to enter the decimal number.
3. Find the remainder when the decimal number is divided by 8.
//find the remainder of the entered decimal number remainder = num % 8;
4. Update octal number by octalNum + (remainder * countVal )
// storing the octalvalue octalNum = (octalNum + (remainder * countVal ));
5. Increase countVal by countVal *10.
//storing exponential value countVal = countVal * 10;
6. Divide the decimal number by 8.
//Divide the num by 8 num = num/8;
7. Repeat the second step until the decimal number is zero.
See the below code to convert decimal numbers to octal numbers in C without using the array.
#include <stdio.h> //typedef to avoid long name typedef unsigned long long ULLINT; // function to calculate the octal value of the given // decimal number ULLINT decimaltoOctal(ULLINT num) { ULLINT octalNum = 0, countval = 1; int remainder = 0; while (num != 0) { // decimals remainder is calculated remainder = num % 8; // storing the octal value octalNum += remainder * countval; // storing exponential value countval = countval * 10; num /= 8; } return octalNum; } int main() { //store decimal number ULLINT num = 0; //store octal number ULLINT octalNum = 0; printf("Enter decimal number: "); scanf("%lld", &num); //Function Call octalNum = decimaltoOctal(num); printf("%lld",octalNum); return 0; }
Output:
Enter a decimal number: 74
112
Example 3: Third way using the math library function
#include <stdio.h> //typedef to avoid long name typedef unsigned long long ULLINT; // Function to return the octal // equivalent of decimal value num ULLINT decimalToOctal(ULLINT num) { // To store the octal number ULLINT binNum = 0; int cnt = 0; while (num != 0) { int rem = num % 8; ULLINT c = pow(10, cnt); binNum += rem * c; num /= 8; // Count used to store exponent value cnt++; } return binNum; } int main() { //num for decimal number ULLINT num; printf("Enter decimal number: "); scanf("%lld", &num); //Called function printf("%lld",decimalToOctal(num)); return 0; }
Output:
Enter a decimal number: 74
112
Recommended Posts for you
- C program to decimal to binary number using recursion.
- C Program to Find the Length of a String using strlen().
- Best Gifts for the programmer and techies.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Memory layout of the C program.
- Internal, External, and None linkage.
- typedef vs #define in C.
- Macro in C, with example code.
- enum in C, you should know.
- 5 ways to reverse bits of an integer.
- Best mouse for programming and coding.