Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple recursive method to convert ints to their binary equivalent.
- *
- * @author Ulizio
- * @version 1
- */
- import java.io.*;
- import java.util.Scanner;
- public class BinaryConverter
- {
- public static void main(String [] args) {
- Scanner getInput = new Scanner(System.in);
- System.out.println("Input an integer to be converted to binary:");
- int num = getInput.nextInt();
- conversionRecursion(num);
- }
- public static void conversionRecursion(int num) {
- //So that nothing funky happens
- if(num > 0) {
- //Recursive Code, continually getting 1s and 0s
- conversionRecursion(num/2);
- //Now using Modulus we can get the remainder when divinding by 2, hence modulus 2.
- //This sections only runs after the recursion has occured.
- //Not println so that they're all on the same line
- System.out.print(num%2);
- //The print statement is effectively the conversion to binary.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement