2020-10-02 15:16:27 +03:00
|
|
|
/**
|
|
|
|
* @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
|
2020-10-02 15:16:27 +03:00
|
|
|
* 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>
|
2020-10-02 15:16:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-02 15:16:27 +03:00
|
|
|
{
|
2020-10-30 19:39:02 +03:00
|
|
|
decimalnum = decimalnum + (octalnum % 10) * pow(8, i);
|
|
|
|
i++;
|
|
|
|
octalnum = octalnum / 10;
|
2020-10-02 15:16:27 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 19:39:02 +03:00
|
|
|
// i is re-initialized
|
2020-10-02 15:16:27 +03:00
|
|
|
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-02 15:16:27 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 19:39:02 +03:00
|
|
|
// Returning the binary number that we got from octal number
|
2020-10-02 15:16:27 +03:00
|
|
|
return binarynum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-30 19:39:02 +03:00
|
|
|
* @brief Main function
|
2020-10-02 15:16:27 +03:00
|
|
|
* @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
|
2020-10-02 15:16:27 +03:00
|
|
|
printf("Equivalent binary number is: %ld", octalToBinary(octalnum));
|
|
|
|
return 0;
|
|
|
|
}
|