TheAlgorithms-C/conversions/octal_to_binary.c

63 lines
1.4 KiB
C
Raw Normal View History

/**
* @brief Octal to binay conversion by scanning user input
* @details
2020-10-30 19:39:02 +03:00
* The octalTobinary function take the octal number as long
* return a long binary nuber after conversion
* @author [Vishnu P](https://github.com/vishnu0pothan)
*/
#include <math.h>
2020-10-30 19:39:02 +03:00
#include <stdio.h>
/**
* @brief Converet octal number to binary
* @param octalnum octal value that need to convert
* @returns A binary number after conversion
*/
long octalToBinary(int octalnum)
{
int decimalnum = 0, i = 0;
long binarynum = 0;
/* This loop converts octal number "octalnum" to the
* decimal number "decimalnum"
*/
2020-10-30 19:39:02 +03:00
while (octalnum != 0)
{
2020-10-30 19:39:02 +03:00
decimalnum = decimalnum + (octalnum % 10) * pow(8, i);
i++;
octalnum = octalnum / 10;
}
2020-10-30 19:39:02 +03:00
// i is re-initialized
i = 1;
/* This loop converts the decimal number "decimalnum" to the binary
* number "binarynum"
*/
while (decimalnum != 0)
{
2020-10-30 19:39:02 +03:00
binarynum = binarynum + (long)(decimalnum % 2) * i;
decimalnum = decimalnum / 2;
i = i * 10;
}
2020-10-30 19:39:02 +03:00
// Returning the binary number that we got from octal number
return binarynum;
}
/**
2020-10-30 19:39:02 +03:00
* @brief Main function
* @returns 0 on exit
*/
int main()
{
int octalnum;
printf("Enter an octal number: ");
scanf("%d", &octalnum);
2020-10-30 19:39:02 +03:00
// Calling the function octaltoBinary
printf("Equivalent binary number is: %ld", octalToBinary(octalnum));
return 0;
}