Created
June 27, 2023 05:32
-
-
Save pmatsinopoulos/98c0ad9cf76191592bdba2b5e9d3d64d 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; | |
import java.util.Stack; | |
public class IntegerToBinary { | |
/** | |
* Takes an integer and returns its binary representation. | |
* It uses a Stack structure to print the digits. | |
* | |
*/ | |
public static String toBinary(int integer) { | |
Stack<Integer> stack = new Stack<Integer>(); | |
while (integer > 0) { | |
int remainder = integer % 2; | |
stack.push(remainder); | |
integer = integer / 2; | |
} | |
String result = ""; | |
while (!stack.isEmpty()) { | |
result = result + stack.pop(); | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
System.out.print("Give me an integer and I will give you back its binary representation: "); | |
Scanner scanner = new Scanner(System.in); | |
if (scanner.hasNextInt()) { | |
int integer = scanner.nextInt(); | |
System.out.printf("Binary representation of %d: is %s\n", integer, toBinary(integer)); | |
} | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment