Created
April 29, 2021 01:18
-
-
Save BT-ICD/8bdd7466519369fa4e515f3e50a5b2a5 to your computer and use it in GitHub Desktop.
Example: Decimal To Octal Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example: Conversion from decimal to octal | |
* Reference: https://www.rapidtables.com/convert/number/decimal-to-binary.html, https://byjus.com/maths/convert-decimal-to-octal/ | |
* Sample Data: | |
* 11 - 13 | |
* 21 - 25 | |
* 57 - 71 | |
* 17 - 21 | |
* | |
* Example 1: Convert (127)10 to Octal. | |
* Solution: Divide 127 by 8 | |
* 127 ÷ 8= 15(Quotient) and (7)Remainder | |
* Divide 15 by 8 again. | |
* 15 ÷ 8 = 1(Quotient) and (7) Remainder | |
* Divide 1 by 8, we get; | |
* 1 ÷ 8 = 0(Quotient) and (1) Remainder | |
* Since the quotient is zero now, no more division can be done. So by taking the remainders in reverse order, we get the equivalent octal number. | |
* Hence, (127)10 = (177)8 | |
**/ | |
public class DecimalToOctalDemo { | |
public static void main(String[] args) { | |
int num=127, rem; | |
StringBuilder ans= new StringBuilder(); | |
while(num>0){ | |
rem = num%8; | |
num=num/8; | |
ans.append(rem); | |
} | |
ans.reverse(); | |
System.out.println(ans); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment