Q:

C program to decimal to octal number without array

belongs to collection: Conversion Programs in C

0

We can convert decimal to octal numbers without using an array. So let 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 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 from the second step until the decimal number is zero.

All Answers

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

#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

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 using the mat... >>
<< C program to decimal to octal number...