2023-09-22 16:25:19 -05:00

87 lines
5.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DecimalToBinary.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.string {color: #6a8759}
.number {color: #6897bb}
.comment {color: #808080}
.whitespace {color: #505050}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP3_Recursion_CalebFontenot/src/mp3_recursion_calebfontenot/DecimalToBinary.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> mp3_recursion_calebfontenot;
<span class="literal">import</span> java.util.Scanner;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> DecimalToBinary {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
Scanner input = <span class="literal">new</span> Scanner(System.in);
<span class="literal">while</span> (<span class="literal">true</span>) {
System.out.print(<span class="string">&quot;</span><span class="string">Enter a decimal integer: </span><span class="string">&quot;</span>);
<span class="literal">int</span> decimal = input.nextInt();
System.out.println(decimal + <span class="string">&quot;</span><span class="string"> is </span><span class="string">&quot;</span> + decimalToBinary(decimal) + <span class="string">&quot;</span><span class="string"> in binary.</span><span class="string">&quot;</span>);
}
}
<span class="literal">public</span> <span class="literal">static</span> String decimalToBinary(<span class="literal">int</span> value) {
<span class="literal">return</span> reverse(decimalToBinary(value, <span class="string">&quot;&quot;</span>));
}
<span class="literal">public</span> <span class="literal">static</span> String decimalToBinary(<span class="literal">int</span> value, String bin) {
<span class="literal">if</span> (value == <span class="number">0</span>) {
<span class="literal">return</span> bin += <span class="string">&quot;&quot;</span>;
} <span class="literal">else</span> <span class="literal">if</span> (value &gt; <span class="number">0</span>) {
<span class="literal">if</span> (value % <span class="number">2</span> == <span class="number">1</span>) {
<span class="literal">return</span> decimalToBinary((<span class="literal">int</span>) Math.floor(value / <span class="number">2</span>), bin + <span class="string">&quot;</span><span class="string">1</span><span class="string">&quot;</span>);
} <span class="literal">else</span> {
<span class="literal">return</span> decimalToBinary((<span class="literal">int</span>) Math.floor(value / <span class="number">2</span>), bin + <span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
}
}
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">0 naht ssel si eulav</span><span class="string">&quot;</span>;
}
<span class="literal">public</span> <span class="literal">static</span> String reverse(String input) {
String returnString = <span class="string">&quot;&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = input.length() - <span class="number">1</span>; <span class="number">0</span> &lt;= i; --i) {
returnString += input.charAt(i);
}
<span class="literal">return</span> returnString;
}
<span class="literal">public</span> <span class="literal">static</span> String decimalToBinaryIterative(<span class="literal">int</span> value) {
String bin = <span class="string">&quot;&quot;</span>;
<span class="literal">while</span> (value &gt; <span class="number">0</span>) {
<span class="literal">if</span> (value % <span class="number">2</span> == <span class="number">1</span>)
bin += <span class="string">&quot;</span><span class="string">1</span><span class="string">&quot;</span>;
<span class="literal">else</span>
bin += <span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>;
value /= <span class="number">2</span>;
}
<span class="literal">return</span> reverse(bin);
}
}
</pre></body>
</html>