Skip to content

Commit

Permalink
Added octal to binary conversion (TheAlgorithms#629)
Browse files Browse the repository at this point in the history
* Added octal to binary conversion

* Update conversions/octal_to_binary.c

Co-authored-by: David Leal <[email protected]>

* Update conversions/octal_to_binary.c

Co-authored-by: David Leal <[email protected]>

* Changes updated

* To trigger action

* updating DIRECTORY.md

* LGTM alert  fixed.

Co-authored-by: David Leal <[email protected]>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 2, 2020
1 parent d810d90 commit a050a48
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [Decimal To Octal Recursion](https://github.com/TheAlgorithms/C/blob/master/conversions/decimal_to_octal_recursion.c)
* [Hexadecimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal.c)
* [Int To String](https://github.com/TheAlgorithms/C/blob/master/conversions/int_to_string.c)
* [Octal To Binary](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_binary.c)
* [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_decimal.c)
* [To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/to_decimal.c)

Expand Down
62 changes: 62 additions & 0 deletions conversions/octal_to_binary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @brief Octal to binay conversion by scanning user input
* @details
* The octalTobinary function take the octal number as long
* return a long binary nuber after conversion
* @author [Vishnu P](https://github.com/vishnu0pothan)
*/
#include <stdio.h>
#include <math.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"
*/
while(octalnum != 0)
{
decimalnum = decimalnum + (octalnum%10) * pow(8,i);
i++;
octalnum = octalnum / 10;
}

//i is re-initialized
i = 1;

/* This loop converts the decimal number "decimalnum" to the binary
* number "binarynum"
*/
while (decimalnum != 0)
{
binarynum = binarynum + (long)(decimalnum % 2) * i;
decimalnum = decimalnum / 2;
i = i * 10;
}

//Returning the binary number that we got from octal number
return binarynum;
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main()
{
int octalnum;

printf("Enter an octal number: ");
scanf("%d", &octalnum);

//Calling the function octaltoBinary
printf("Equivalent binary number is: %ld", octalToBinary(octalnum));
return 0;
}

0 comments on commit a050a48

Please sign in to comment.