-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryConverter.java
90 lines (73 loc) · 3.35 KB
/
BinaryConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
public class BinaryConverter
{//start of class
int number;//to store input decimal number
static long binNumber;//to store resultant binary number
BinaryConverter(int n){//start of constructor
number=n;
binNumber=0;
}
long convert(int n){//recursive method to convert decimal to binary
if (n==0){
return n;
}
else{
int r=n%2;
return convert (n/2)*10+ r;//maintaining a stack and calling itself
}
}
void display (){//to display the binary number
System.out.println("The original decimal number is:\t"+ number);
System.out.println("The resultant binary number is:"+ binNumber);
}
public static void main (String args[]) throws IOException{//start of main method
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter a decimal number to be converted");
int n=Integer.parseInt(br.readLine());//to store the input integer
BinaryConverter ob= new BinaryConverter (n);//creating an object
binNumber=ob.convert(n);//to store equivalent binary number
ob.display();//to display the result
}// end of main method
}//end of class
/*
* Variable Description Table
*
* Variable Name Datatype Description
* n int to store input value
* number int to store the input decimal number
* binNumber long to store the equivalent binary number
* r int to store the remainder of n and 2
*/
/*
Binary Converter: To input a decimal number and print its binary equivalent through recursion.
Algorithm for BinaryConverter ():
Step 1: Start of algorithm for BinaryConverter ().
Step 2: Initialize the class variables number and binNumber to 0.
Step 3: End of algorithm of BinaryConverter ().
Algorithm for convert ():
Step 1: Start of algorithm for convert ().
Step 2: if n = 0 then return n
Step 3: else store the remainder of n and 2 in an integer variable (say r)
Step 4: return (n / 2) x 10 + r to the method itself.
Step 5: End of algorithm for convert ().
Algorithm for display ():
Step 1: Start of algorithm for display ().
Step 2: Print the original decimal number and the equivalent binary number.
Step 3: End of algorithm for display ().
Algorithm for main method ():
Step 1: Start of algorithm for main method ().
Step 2: Input a decimal number in an integer variables (n).
Step 3: Create an object and pass n to the constructer.
Step 4: Call method convert using the object created and pass variable n and store the returned value in binNumber (class variable).
Step 5: Call the method display () using the created object.
Step 6: End of algorithm of main method ().
*/
/*
👋 Hi, I’m @aarushinair - Aarushi Nair (she/her/ella)
👀 I’m a Computer Science Engineering Student
💞️ I’m looking to collaborate on #java, #python, #R, #applicationdevelopment
🌱 #GirlsWhoCode #WomenInTech #WomenInIT #WomenInSTEM #CyberSecurity #QuantumComputing #BlockChain #AI #ML
📫 How to reach me: https://www.linkedin.com/in/aarushinair/
👩🏫 YouTube Channel - Code with Aarushi : https://www.youtube.com/channel/UCKj5T1ELHCmkGKujkpqtl7Q
🙋 Follow me on Twitter: https://twitter.com/aarushinair_
*/