A command-line tool that converts decimal numbers (including fractions) to binary representation. Built with Node.js using recursion and implementing proper mathematical conversion algorithms.
This CLI application converts decimal numbers to binary format using mathematical algorithms implemented through recursive functions. It handles both integers and fractional decimal numbers, demonstrating core programming concepts like recursion, input validation, and number system conversion.
- ✅ Handles Integers AND Fractions - Not just whole numbers
- ✅ Pure Recursion - Mathematical algorithms implemented recursively
- ✅ Input Validation - Checks for valid decimal numbers
- ✅ Educational - Clean code demonstrating recursion concepts
- 🔄 Recursive Conversion - Uses mathematical recursion for both integer and fractional parts
- 🔢 Decimal to Binary - Accurate conversion of decimal numbers
- 💯 Fraction Support - Converts decimal fractions (e.g., 5.75 → 101.11)
- ✅ Input Validation - Prevents invalid inputs (negative numbers, non-numbers, empty strings)
- 🎨 Clean CLI Interface - Simple and straightforward user experience
- 📚 Educational Code - Clear implementation of conversion algorithms
$ node app.mjs
Enter the number: 10
You have entered 10
The binary equivalent of 10 is: 1010
$ node app.mjs
Enter the number: 5.75
You have entered 5.75
The binary equivalent of 5.75 is: 101.11
$ node app.mjs
Enter the number: -5
Please input a decimal number greater than 0
Enter the number: 12.5
You have entered 12.5
The binary equivalent of 12.5 is: 1100.1The algorithm divides the number by 2 repeatedly and keeps track of remainders:
Example: 10 (decimal) → 1010 (binary)
10 ÷ 2 = 5 remainder 0
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Reading remainders from bottom to top: 1010
The algorithm multiplies the fraction by 2 repeatedly and tracks the integer part:
Example: 0.75 (decimal) → 0.11 (binary)
0.75 × 2 = 1.5 → integer part: 1, fraction: 0.5
0.5 × 2 = 1.0 → integer part: 1, fraction: 0.0
Reading integer parts: 0.11
- Node.js - JavaScript runtime
- ES6 Modules (.mjs) - Modern JavaScript syntax
- prompt-sync - User input handling
- Recursion - Core algorithm implementation
- Node.js (v12.0.0 or higher)
- npm (Node Package Manager)
Check your installation:
node --version
npm --versiongit clone https://github.com/rishiverma12031/BinaryConverter.git
cd BinaryConverternpm install prompt-syncnode app.mjs- Run the application:
node app.mjs - Enter a decimal number when prompted (positive numbers only)
- View the binary result displayed in the terminal
- Whole numbers:
10,255,1024 - Decimal fractions:
5.5,12.75,0.625 - Zero:
0
- Negative numbers:
-5 - Non-numeric values:
abc,12a - Empty input:
BinaryConverter/
│
├── app.mjs # Main application file
├── package.json # Project dependencies (create this)
└── README.md # Documentation
- Main conversion function
- Splits number into integer and fractional parts
- Combines binary results from both parts
- Recursive function for converting integer part
- Base case: Returns when number is 0 or 1
- Recursive case: Divides by 2 and processes remainder
- Recursive function for converting fractional part
- Base case: Returns when fraction becomes 0
- Recursive case: Multiplies by 2 and tracks integer part
- Validates user input
- Checks for negative numbers, NaN, and empty strings
- Recursive - Re-prompts on invalid input
This project demonstrates understanding of:
- ✅ Recursion - Multiple recursive functions with proper base cases
- ✅ Number Systems - Understanding binary and decimal conversion
- ✅ Mathematical Algorithms - Implementing standard conversion methods
- ✅ Type Coercion - String/Number conversions and type handling
- ✅ Input Validation - Using isNaN(), comparison operators
- ✅ String Manipulation - split(), String() conversions
- ✅ ES6 Modules - Modern import/export syntax
- Clean function separation
- Recursive algorithm design
- Error handling and validation
- User-friendly CLI interface
Try these inputs to test the converter:
| Decimal | Expected Binary | Description |
|---|---|---|
| 0 | 0 | Zero |
| 1 | 1 | One |
| 10 | 1010 | Simple integer |
| 255 | 11111111 | 8-bit max value |
| 0.5 | 0.1 | Simple fraction |
| 0.75 | 0.11 | Three-quarters |
| 5.75 | 101.11 | Mixed number |
| 12.5 | 1100.1 | Decimal with .5 |
function convertToBinaryInteger(decimalNum) {
// Base case: 0 or 1 can't be divided further
if (decimalNum === 0 || decimalNum === 1) return String(decimalNum);
// Recursive case: divide by 2 and concatenate remainder
return convertToBinaryInteger(Math.floor(decimalNum / 2)) + String(decimalNum % 2);
}How it works:
- Divide number by 2 using
Math.floor()for integer division - Get remainder using modulo operator
% - Recursively process quotient
- Concatenate remainders in reverse order
function convertToBinaryFraction(decimalNum) {
// Base case: fraction becomes 0
if(decimalNum === 0) return String(decimalNum);
// Recursive case: multiply by 2 and track integer part
return String(Math.floor(decimalNum * 2)) +
convertToBinaryFraction(decimalNum * 2 - Math.floor(decimalNum * 2));
}How it works:
- Multiply fraction by 2
- Extract integer part (0 or 1) using
Math.floor() - Get new fraction part (remainder after extracting integer)
- Recursively process new fraction
- Concatenate results in order
- Add support for other number bases (octal, hexadecimal)
- Display step-by-step conversion process
- Add option to convert binary back to decimal
- Implement iterative versions alongside recursive
- Add command-line arguments for batch conversion
- Output conversion steps for educational purposes
- Support multiple number formats as input
- Add file input/output for bulk conversions
- Create a GUI version with Electron
- Add support for negative numbers (two's complement)
- Implement scientific notation support
- Build a web version with conversion visualization
Error: "Cannot find module 'prompt-sync'"
npm install prompt-syncError: "SyntaxError: Cannot use import statement outside a module"
- Ensure file extension is
.mjs, OR - Add
"type": "module"topackage.json
Infinite loop with fractions
- This occurs with repeating binary fractions (like 0.1 decimal)
- Consider adding a precision limit in production code
Input validation not working
- Check that
isNaN()is used correctly - Ensure comparison operators are appropriate (
<for negative check)
- Add precision control for fractional conversions
- Implement reverse conversion (binary to decimal)
- Support for other number systems (octal, hex)
- Add step-by-step explanation mode
- Create batch conversion from file
- Build interactive web interface
- Add negative number support (two's complement)
- Include scientific notation handling
- Add unit tests
- Implement conversion history
Contributions are welcome! Here's how:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
Rishi Verma
- GitHub: @rishiverma12031
- Project Link: https://github.com/rishiverma12031/BinaryConverter
- Built to learn and practice recursion and mathematical algorithms
- Implements standard decimal-to-binary conversion methods
- Uses prompt-sync for user input
- Binary Number System - Wikipedia
- Recursion in JavaScript - MDN
- Number.prototype.toString(2) - Alternative Method
- Binary Conversion Algorithms
While JavaScript provides Number.toString(2) for binary conversion, this project implements the algorithm from scratch using recursion to demonstrate:
- How recursion solves mathematical problems
- Understanding of number system conversion
- Implementation of standard conversion algorithms
- Practical application of recursive thinking
Ready to convert? Run node app.mjs and start converting! 🔢
Built with ❤️ while learning recursion and mathematical algorithms in JavaScript