Before diving into the C program to convert a decimal number to octal, let's briefly discuss the numerical base systems and their representation.
Numerical Base Systems
In mathematics and computer science, we frequently use different numerical base systems to represent numbers. The most common base systems are:
Decimal (Base 10): This is the base system we use in our day-to-day lives. It uses ten symbols (0-9) to represent numbers. For example, 123 in decimal means 110^2 + 210^1 + 3*10^0 = 100 + 20 + 3 = 123.
Binary (Base 2): In the binary system, there are only two symbols, 0 and 1. Computers often use binary because electronic circuits can easily represent these two states.
Octal (Base 8): Octal uses eight symbols, 0-7. It's used in some computer programming contexts, especially in older systems. Each octal digit corresponds to three binary digits, making it convenient for some low-level programming.
Hexadecimal (Base 16): Hexadecimal uses sixteen symbols, 0-9 and A-F. It is widely used in computer science to represent binary data more compactly. Each hexadecimal digit corresponds to four binary digits.
Converting Decimal to Octal
To convert a decimal number to octal, we need to repeatedly divide the decimal number by 8 and record the remainders at each step. These remainders, when read in reverse order, will give us the octal representation of the decimal number.
Now, let's look at the C program that accomplishes this conversion. The program takes a decimal number as input, performs the conversion, and displays the octal equivalent.
Here's a C program that converts a decimal number to its octal representation. I'll explain each part of the code and provide an example with its output.
#include <stdio.h>
int main() {
int decimalNumber, remainder, quotient;
int octalNumber[100], i = 1;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
quotient = decimalNumber;
while (quotient != 0) {
octalNumber[i++] = quotient % 8;
quotient = quotient / 8;
}
printf("Octal equivalent: ");
for (int j = i - 1; j > 0; j--) {
printf("%d", octalNumber[j]);
}
return 0;
}
Let's break down the code step by step:
We include the standard input/output library (
stdio.h
) to use functions likeprintf
andscanf
.In the
main
function, we declare the following variables:decimalNumber
: This is the decimal number entered by the user.remainder
andquotient
: These are temporary variables for calculations.octalNumber[100]
: An array to store the octal digits.i
: A counter for theoctalNumber
array.
We prompt the user to enter a decimal number and store it in the
decimalNumber
variable.We initialize
quotient
with the value ofdecimalNumber
. This is the number we will be converting to octal.We use a
while
loop to perform the conversion. Inside the loop:remainder
stores the remainder of thequotient
when divided by 8, which is the next octal digit.- We store the remainder in the
octalNumber
array and incrementi
. - We update the
quotient
by dividing it by 8 to proceed to the next digit.
After the loop, we print the octal equivalent by iterating through the
octalNumber
array in reverse order and printing each digit.
Now, let's run an example:
Enter a decimal number: 123
Octal equivalent: 173
In this example, we input the decimal number 123, and the program converts it to its octal equivalent, which is 173.