#include <stdio.h>

void decimalToOctal(int decimal) {
    int octal[50]; // Array to store octal digits
    int index = 0; // Index for octal array

    // Continue dividing the decimal number by 8
    while (decimal > 0) {
        octal[index] = decimal % 8; // Store the remainder
        decimal = decimal / 8;       // Update the decimal number
        index++;                      // Move to the next index
    }

    // Print the octal number in reverse order
    printf("Octal equivalent: ");
    for (int i = index - 1; i >= 0; i--) {
        printf("%d", octal[i]); // Print stored remainders
    }
    printf("\n"); // New line after output
}

int main() {
    int decimal;

    // Get user input
    printf("Enter a decimal number: ");
    scanf("%d", &decimal); // Read the decimal number

    // Call the conversion function
    decimalToOctal(decimal);

    return 0; // Indicate successful completion
}

Explanation:

Purpose of the Program

The main goal of this program is to take a decimal (base-10) number as input and convert it to its octal (base-8) representation. This involves breaking down the decimal number into parts that can be expressed using only the digits 0 through 7, which are used in the octal system.

Key Concepts

  1. Decimal System:
    • The decimal system is the number system most commonly used in everyday life. It uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
    • Each digit’s position in a number represents a power of 10.
  2. Octal System:
    • The octal system uses eight digits: 0, 1, 2, 3, 4, 5, 6, 7.
    • Each digit’s position in an octal number represents a power of 8.

Output:

Enter a decimal number: 65

Octal equivalent: 101

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights