Update octal_to_binary.c

This commit is contained in:
Suraj Patro 2020-10-30 22:09:02 +05:30 committed by GitHub
parent b282b95d5b
commit f23987e8ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 13 deletions

View File

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