Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created September 16, 2021 00:44
Show Gist options
  • Save jananpatel2002/e5e83198733a8b27e8fd17647d5f2be4 to your computer and use it in GitHub Desktop.
Save jananpatel2002/e5e83198733a8b27e8fd17647d5f2be4 to your computer and use it in GitHub Desktop.
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