How to convert Decimal to Octal and Vice-versa in C || Type 3 || Functions || C programming
In this program, we are going to see how to Convert Decimal to Octal and Vice-versa (Type-3) in C Programming Language.

 

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

long long octalToDecimal()
{
    int decimalNumber = 0, i = 0, octalNumber;
    printf("Enter the Octal Number: ");
    scanf("%d", &octalNumber);

    while (octalNumber != 0)
    {
        decimalNumber += (octalNumber % 10) * pow(8, i);
        ++i;
        octalNumber /= 10;
    }

    i = 1;

    return decimalNumber;
}

int decimalToOctal()
{
    int octalNumber = 0, i = 1, decimalNumber;
    printf("Enter the Decimal Number: ");
    scanf("%d", &decimalNumber);

    while (decimalNumber != 0)
    {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}

int main()
{
    int operation, decimal;
    int octal;
    printf("-----Select the Operation------\n");
    printf("1. Octal to Decimal\n2. Decimal to Octal\n3. Exit\n");
    printf("Enter the corresponding number of Operation: ");
    scanf("%d", &operation);

    switch (operation)
    {
    case 1:
        printf("%lld in decimal", octalToDecimal());
        break;

    case 2:
        printf("%d in Octal", decimalToOctal());
        break;

    case 3:
        exit(0);

    default:
        printf("Wrong input");
    }

    return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post