Created
September 16, 2021 00:44
-
-
Save jananpatel2002/e5e83198733a8b27e8fd17647d5f2be4 to your computer and use it in GitHub Desktop.
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
import java.util.Scanner; | |
public class DecimalToOctal { | |
public static void main(String[] args) { | |
// Initializing a scanner object so the user can write a number | |
@SuppressWarnings("resource") | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Write an integer that you would like to change into an octal number: "); | |
// x will equal to the users input | |
int x = sc.nextInt(); | |
// array's remainder will be initialized | |
int array_value; | |
String finalAnswer = ""; | |
// Initializing an array of characters 0-7 since that's what octals use | |
char octalNumbers[] = { '0', '1', '2', '3', '4', '5', '6', '7' }; | |
// initializing a message before the X value changes | |
String message = ("The octal converstion for " + x + " = "); | |
// while loop stops when the x value is 0 | |
while (x != 0) { | |
array_value = x % 8; | |
finalAnswer = octalNumbers[array_value] + finalAnswer; | |
x = x / 8; | |
} | |
// prints out the final message | |
System.out.print(message + finalAnswer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment