Q:

C program to decimal to octal number

belongs to collection: Conversion Programs in C

0

In this exercise, you will learn to convert decimal number to octal. 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 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to decimal to octal number without array... >>
<< C program to convert decimal to binary without usi...