From 7634c4e946986d5971ddd9c7cefe1841c92171ba Mon Sep 17 00:00:00 2001 From: bjv33 <bjv33@cs.drexel.edu> Date: Thu, 25 Oct 2018 13:25:23 -0400 Subject: [PATCH] Binary Representation -- Recursion --- Binary_Representation.java | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Binary_Representation.java diff --git a/Binary_Representation.java b/Binary_Representation.java new file mode 100644 index 0000000..d18cd48 --- /dev/null +++ b/Binary_Representation.java @@ -0,0 +1,51 @@ +// Program: Binary_Representation.java +// Purpose: Writes a recursive method that converts a number from its decimal representation to its binary representation. +// Author: Brian Vojtko +// Date: 10/24/2018 +import java.util.Scanner; + +public class Binary_Representation { + + public static void binConv(int number) { + + if (number > 0 ) { + binConv(number / 2); + System.out.print(number % 2); + } + } + public static void main(String[] args) { + + Scanner input = new Scanner(System.in); + + boolean loop = true; + + while(loop) { + + System.out.print("Enter a positive integer or 0 to end: "); + int number = input.nextInt(); + + while (number < 0) { + System.out.println("Error: You entered a negative value. Try again."); + number = input.nextInt(); + continue; + } + + if (number == 0) { + System.out.println("Goodbye"); + return; + } + + System.out.print(number + " in binary is "); + binConv(number); + + if (number >= 1) { + loop = true; + System.out.println(""); + } + else { + loop = false; + } + } + } +} + -- GitLab