From d49b8766451ba8c044114bce0f65c48f71b967ea Mon Sep 17 00:00:00 2001
From: Sarthak950
Date: Wed, 4 Oct 2023 19:43:55 +0530
Subject: [PATCH 1/6] added 404 and Three js in to web markdown
---
Internet/readme.md | 25 +++++++++++++++++++++
Web Technology/WebTechnology.md | 39 +++++++++++++++++----------------
2 files changed, 45 insertions(+), 19 deletions(-)
diff --git a/Internet/readme.md b/Internet/readme.md
index 003b1a6f..1da56b0e 100644
--- a/Internet/readme.md
+++ b/Internet/readme.md
@@ -49,3 +49,28 @@ The Internet Protocol (IP) is a protocol, or set of rules, for routing and addre
- [Internet of Things (IoT)](IOT/readme.md)
- [Cyber Crime](Cyber%20Crimes/readme.md)
+## Error Codes
+While browsing the internet, you may have encountered some error codes.
+Like:
+* 404 Not Found
+* 403 Forbidden
+* 500 Internal Server Error
+* 502 Bad Gateway
+* 503 Service Unavailable
+* 504 Gateway Timeout
+* 408 Request Timeout
+
+These errors might be confusing for you. But they make the life of the developer a lot easier
+as they contain a lot of useful information about the problem.
+
+In genral the error codes means:
+* 1xx: informational response - it means the request was received and the process is continuing
+* 2xx: success - the request was successfully received, understood, and accepted
+* 3xx: redirection - further action needs to be taken in order to complete the request
+* 4xx: client error - the request contains bad syntax or cannot be fulfilled
+* 5xx: server error - the server failed to fulfil an apparently valid request
+
+
+### 404 Not Found
+This error means that the requested resource could not be found. This is the most common error you will encounter while browsing the internet. This error is returned when the server is unable to find the requested resource. This error can be caused by a number of things, including a broken link, a mistyped URL, or a deleted file.
+
diff --git a/Web Technology/WebTechnology.md b/Web Technology/WebTechnology.md
index 8ff1b4ca..fa906d3c 100644
--- a/Web Technology/WebTechnology.md
+++ b/Web Technology/WebTechnology.md
@@ -20,7 +20,6 @@ A webpage is a digital document that is linked to the World Wide Web and viewabl
Web Development
Web development refers to the building, creating, and maintaining of websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet i.e. websites.
-
Web Development can be classified into two ways:
Frontend Development
The part of a website that the user interacts directly is termed as front end. It is also referred to as the ‘client side’ of the application.
@@ -28,7 +27,7 @@ Web development refers to the building, creating, and maintaining of websites. I
Backened Development
Backend is the server side of a website. It is the part of the website that users cannot see and interact. It is the portion of software that does not come in direct contact with the users. It is used to store and arrange data.
-Frontend Languages
+Frontend Languages
The front end portion is built by using some languages which are discussed below:
HTML
@@ -44,13 +43,14 @@ Web development refers to the building, creating, and maintaining of websites. I
There are many other languages through which one can do front-end development depending upon the framework for example Flutter user Dart, React uses JavaScript and Django uses Python, and much more.
Front End Frameworks and Libraries
-
+
Backend Languages
-The back end portion is built by using some languages which are discussed below:
-
+The back end portion is built by using some languages which are discussed below:
PHP
PHP is a server-side scripting language designed specifically for web development. Since PHP code executed on the server-side, so it is called a server-side scripting language.
@@ -84,26 +84,26 @@ The software which is used to manage database is called Database Management Syst
-1. Hierarchical Databases :
-Just as in any hierarchy, this database follows the progression of data being categorized in ranks or levels, wherein data is categorized based on a common point of linkage. As a result, two entities of data will be lower in rank and the commonality would assume a higher rank. Refer to the diagram below:
+1. Hierarchical Databases :
+Just as in any hierarchy, this database follows the progression of data being categorized in ranks or levels, wherein data is categorized based on a common point of linkage. As a result, two entities of data will be lower in rank and the commonality would assume a higher rank. Refer to the diagram below:
-2. Network Databases :
-In Layman’s terms, a network database is a hierarchical database, but with a major tweak. The child records are given the freedom to associate with multiple parent records. As a result, a network or net of database files linked with multiple threads is observed. Notice how the Student, Faculty, and Resources elements each have two-parent records, which are Departments and Clubs.
+2. Network Databases :
+In Layman’s terms, a network database is a hierarchical database, but with a major tweak. The child records are given the freedom to associate with multiple parent records. As a result, a network or net of database files linked with multiple threads is observed. Notice how the Student, Faculty, and Resources elements each have two-parent records, which are Departments and Clubs.
-3. Object-Oriented Databases :
-Those familiar with the Object-Oriented Programming Paradigm would be able to relate to this model of databases easily. Information stored in a database is capable of being represented as an object which response as an instance of the database model. Therefore, the object can be referenced and called without any difficulty. As a result, the workload on the database is substantially reduced.
+3. Object-Oriented Databases :
+Those familiar with the Object-Oriented Programming Paradigm would be able to relate to this model of databases easily. Information stored in a database is capable of being represented as an object which response as an instance of the database model. Therefore, the object can be referenced and called without any difficulty. As a result, the workload on the database is substantially reduced.
-4. Relational Databases :
-Considered the most mature of all databases, these databases lead in the production line along with their management systems. In this database, every piece of information has a relationship with every other piece of information. This is on account of every data value in the database having a unique identity in the form of a record.
+4. Relational Databases :
+Considered the most mature of all databases, these databases lead in the production line along with their management systems. In this database, every piece of information has a relationship with every other piece of information. This is on account of every data value in the database having a unique identity in the form of a record.
-5. NoSQL Databases :
-A NoSQL originally referring to non SQL or non-relational is a database that provides a mechanism for storage and retrieval of data. This data is modeled in means other than the tabular relations used in relational databases.
+5. NoSQL Databases :
+A NoSQL originally referring to non SQL or non-relational is a database that provides a mechanism for storage and retrieval of data. This data is modeled in means other than the tabular relations used in relational databases.
@@ -114,9 +114,9 @@ Format of data is used by web applications to communicate with each other. It is
XML
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.
-JSON
+JSON
JSON or JavaScript Object Notation is a format for structuring data.
-API
+API
API is an abbreviation for Application Programming Interface which is a collection of communication protocols and subroutines used by various programs to communicate between them.
Web Protocols
@@ -138,8 +138,9 @@ Graphical elements are one of the key feature of any webpage. They can be used t
Canvas
The HTML “canvas” element is used to draw graphics via JavaScript.
+THREE.Js
+
P5.Js
+
SVG
SVG stands for Scalable Vector Graphics.
It basically defines vector-based graphics in XML format.
-
-
From 824e4f0fae0838e1e956249f7597201cac982efb Mon Sep 17 00:00:00 2001
From: Sarthak950
Date: Fri, 6 Oct 2023 11:17:10 +0530
Subject: [PATCH 2/6] added Number system conversion for 6 languages C, C++,
JS, TS, Lua, Java
---
Number System/Conversion_In_C.c | 346 +++++++++++++++++++++++++
Number System/Conversion_In_JS.js | 311 +++++++++++++++++++++++
Number System/Conversion_In_Java.java | 347 ++++++++++++++++++++++++++
Number System/Conversion_In_TS.ts | 342 +++++++++++++++++++++++++
Number System/Conversion_In_c++.cpp | 341 +++++++++++++++++++++++++
Number System/Convesion_In_Lua.lua | 339 +++++++++++++++++++++++++
6 files changed, 2026 insertions(+)
create mode 100644 Number System/Conversion_In_C.c
create mode 100644 Number System/Conversion_In_JS.js
create mode 100644 Number System/Conversion_In_Java.java
create mode 100644 Number System/Conversion_In_TS.ts
create mode 100644 Number System/Conversion_In_c++.cpp
create mode 100644 Number System/Convesion_In_Lua.lua
diff --git a/Number System/Conversion_In_C.c b/Number System/Conversion_In_C.c
new file mode 100644
index 00000000..6c89a85e
--- /dev/null
+++ b/Number System/Conversion_In_C.c
@@ -0,0 +1,346 @@
+/*
+Author: Sarthak Sharma
+Github: https://github.com/Sarthak950
+Website: https://sarthak950.netlify.app/
+
+This program contains functions to convert a number from one base to another.
+The bases supported are decimal, binary, octal and hexadecimal.
+
+The functions are:
+1. decToBin(int n) - Convert decimal to binary
+2. decToOct(int n) - Convert decimal to octal
+3. decToHex(int n) - Convert decimal to hexadecimal
+4. binToDec(int n) - Convert binary to decimal
+5. octToDec(int n) - Convert octal to decimal
+6. hexToDec(char *hexVal) - Convert hexadecimal to decimal
+7. binToOct(int n) - Convert binary to octal
+8. binToHex(int n) - Convert binary to hexadecimal
+9. octToBin(int n) - Convert octal to binary
+10. octToHex(int n) - Convert octal to hexadecimal
+11. hexToBin(char *hexVal) - Convert hexadecimal to binary
+12. hexToOct(char *hexVal) - Convert hexadecimal to octal
+
+Also this is a special function that convert ant number from one base to another aka universal convertor
+* convert(char *num, int base1, int base2) - Convert a number from one base to another
+
+*/
+
+
+#include
+#include
+#include
+
+// Function to convert decimal to binary
+void decToBin(int n) {
+ int binaryNum[32];
+ int i = 0;
+ while (n > 0) {
+ binaryNum[i] = n % 2;
+ n = n / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%d", binaryNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert decimal to octal
+void decToOct(int n) {
+ int octalNum[100];
+ int i = 0;
+ while (n != 0) {
+ octalNum[i] = n % 8;
+ n = n / 8;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%d", octalNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert decimal to hexadecimal
+void decToHex(int n) {
+ char hexaDeciNum[100];
+ int i = 0;
+ while (n != 0) {
+ int temp = 0;
+ temp = n % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = temp + 48;
+ i++;
+ } else {
+ hexaDeciNum[i] = temp + 55;
+ i++;
+ }
+ n = n / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%c", hexaDeciNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert binary to decimal
+void binToDec(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ printf("%d\n", decimal);
+}
+
+// Function to convert octal to decimal
+void octToDec(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ printf("%d\n", decimal);
+}
+
+// Function to convert hexadecimal to decimal
+void hexToDec(char *hexVal) {
+ int len = strlen(hexVal);
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal[i] - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal[i] - 55) * base;
+ base = base * 16;
+ }
+ }
+ printf("%d\n", dec_val);
+}
+
+// Function to convert binary to octal
+void binToOct(int n) {
+ int octal = 0, decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ power = 1;
+ while (decimal > 0) {
+ int lastDigit = decimal % 8;
+ octal += lastDigit * power;
+ power *= 10;
+ decimal /= 8;
+ }
+ printf("%d\n", octal);
+}
+
+// Function to convert binary to hexadecimal
+void binToHex(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ char hexaDeciNum[100];
+ int i = 0;
+ while (decimal != 0) {
+ int temp = 0;
+ temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = temp + 48;
+ i++;
+ } else {
+ hexaDeciNum[i] = temp + 55;
+ i++;
+ }
+ decimal = decimal / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%c", hexaDeciNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert octal to binary
+void octToBin(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ int binaryNum[32];
+ int i = 0;
+ while (decimal > 0) {
+ binaryNum[i] = decimal % 2;
+ decimal = decimal / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%d", binaryNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert octal to hexadecimal
+void octToHex(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ char hexaDeciNum[100];
+ int i = 0;
+ while (decimal != 0) {
+ int temp = 0;
+ temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = temp + 48;
+ i++;
+ } else {
+ hexaDeciNum[i] = temp + 55;
+ i++;
+ }
+ decimal = decimal / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%c", hexaDeciNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert hexadecimal to binary
+void hexToBin(char *hexVal) {
+ int len = strlen(hexVal);
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal[i] - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal[i] - 55) * base;
+ base = base * 16;
+ }
+ }
+ int binaryNum[32];
+ int i = 0;
+ while (dec_val > 0) {
+ binaryNum[i] = dec_val % 2;
+ dec_val = dec_val / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%d", binaryNum[j]);
+ }
+ printf("\n");
+}
+
+// Function to convert hexadecimal to octal
+void hexToOct(char *hexVal) {
+ int len = strlen(hexVal);
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal[i] - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal[i] - 55) * base;
+ base = base * 16;
+ }
+ }
+ int octalNum[100];
+ int i = 0;
+ while (dec_val != 0) {
+ octalNum[i] = dec_val % 8;
+ dec_val = dec_val / 8;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ printf("%d", octalNum[j]);
+ }
+ printf("\n");
+}
+
+
+
+// Function to convert a number from one base to another
+void convert(char *num, int base1, int base2) {
+ int decimal = 0, power = 1, len = strlen(num);
+ for (int i = len - 1; i >= 0; i--) {
+ int digit;
+ if (num[i] >= '0' && num[i] <= '9') {
+ digit = num[i] - '0';
+ } else if (num[i] >= 'A' && num[i] <= 'Z') {
+ digit = num[i] - 'A' + 10;
+ } else if (num[i] >= 'a' && num[i] <= 'z') {
+ digit = num[i] - 'a' + 10;
+ } else {
+ printf("Invalid input!\n");
+ return -1;
+ }
+ if (digit >= base1) {
+ printf("Invalid input!\n");
+ return -1;
+ }
+ decimal += digit * power;
+ power *= base1;
+ }
+ char *result = malloc(sizeof(char) * 100);
+ int i = 0;
+ while (decimal > 0) {
+ int digit = decimal % base2;
+ if (digit < 10) {
+ result[i++] = digit + '0';
+ } else {
+ result[i++] = digit - 10 + 'A';
+ }
+ decimal /= base2;
+ }
+ result[i] = '\0';
+ strrev(result);
+ printf("Result: %s\n", result);
+ free(result);
+ return;
+}
+
+int main() {
+ // Example usage of the conversion functions
+ int decimalNumber = 42;
+ char *hexValue = "2A";
+ int binaryValue = 11010;
+ int octalValue = 52;
+
+ decToBin(decimalNumber);
+ decToOct(decimalNumber);
+ decToHex(decimalNumber);
+ binToDec(binaryValue);
+ octToDec(octalValue);
+ hexToDec(hexValue);
+ binToOct(binaryValue);
+ binToHex(binaryValue);
+ octToBin(octalValue);
+ octToHex(octalValue);
+ hexToBin(hexValue);
+ hexToOct(hexValue);
+
+ convert("1A", 16, 2); // Convert hex "1A" to binary
+ convert("1101", 2, 10); // Convert binary "1101" to decimal
+ convert("57", 8, 16); // Convert octal "57" to hexadecimal
+
+ return 0;
+}
diff --git a/Number System/Conversion_In_JS.js b/Number System/Conversion_In_JS.js
new file mode 100644
index 00000000..cc11c682
--- /dev/null
+++ b/Number System/Conversion_In_JS.js
@@ -0,0 +1,311 @@
+/*
+Author: Sarthak Sharma
+Github: https://github.com/Sarthak950
+Website: https://sarthak950.netlify.app/
+
+This program contains functions to convert a number from one base to another.
+The bases supported are decimal, binary, octal and hexadecimal.
+
+The functions are:
+1. decToBin(n) - Convert decimal to binary
+2. decToOct(n) - Convert decimal to octal
+3. decToHex(n) - Convert decimal to hexadecimal
+4. binToDec(n) - Convert binary to decimal
+5. octToDec(n) - Convert octal to decimal
+6. hexToDec(hexVal) - Convert hexadecimal to decimal
+7. binToOct(n) - Convert binary to octal
+8. binToHex(n) - Convert binary to hexadecimal
+9. octToBin(n) - Convert octal to binary
+10. octToHex(n) - Convert octal to hexadecimal
+11. hexToBin(hexVal) - Convert hexadecimal to binary
+12. hexToOct(hexVal) - Convert hexadecimal to octal
+
+There is a special function
+* convert(num, base1, base2) - Convert a number from one base to another
+This function converts any number from base1 to base2.
+aka Universal converter
+
+*/
+
+// Function to convert decimal to binary
+function decToBin(n) {
+ let binaryNum = [];
+ let i = 0;
+ while (n > 0) {
+ binaryNum[i] = n % 2;
+ n = Math.floor(n / 2);
+ i++;
+ }
+ console.log(binaryNum.reverse().join(''));
+}
+
+// Function to convert decimal to octal
+function decToOct(n) {
+ let octalNum = [];
+ let i = 0;
+ while (n !== 0) {
+ octalNum[i] = n % 8;
+ n = Math.floor(n / 8);
+ i++;
+ }
+ console.log(octalNum.reverse().join(''));
+}
+
+// Function to convert decimal to hexadecimal
+function decToHex(n) {
+ let hexaDeciNum = [];
+ let i = 0;
+ while (n !== 0) {
+ let temp = n % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = String.fromCharCode(temp + 48);
+ } else {
+ hexaDeciNum[i] = String.fromCharCode(temp + 55);
+ }
+ i++;
+ n = Math.floor(n / 16);
+ }
+ console.log(hexaDeciNum.reverse().join(''));
+}
+
+// Function to convert binary to decimal
+function binToDec(n) {
+ let decimal = 0;
+ let power = 1;
+ while (n > 0) {
+ const lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n = Math.floor(n / 10);
+ }
+ console.log(decimal);
+}
+
+// Function to convert octal to decimal
+function octToDec(n) {
+ let decimal = 0;
+ let power = 1;
+ while (n > 0) {
+ const lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n = Math.floor(n / 10);
+ }
+ console.log(decimal);
+}
+
+// Function to convert hexadecimal to decimal
+function hexToDec(hexVal) {
+ let len = hexVal.length;
+ let base = 1;
+ let dec_val = 0;
+ for (let i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal.charCodeAt(i) - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal.charCodeAt(i) - 55) * base;
+ base = base * 16;
+ }
+ }
+ console.log(dec_val);
+}
+
+// Function to convert binary to octal
+function binToOct(n) {
+ let octal = 0;
+ let decimal = 0;
+ let power = 1;
+ while (n > 0) {
+ const lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n = Math.floor(n / 10);
+ }
+ power = 1;
+ while (decimal > 0) {
+ const lastDigit = decimal % 8;
+ octal += lastDigit * power;
+ power *= 10;
+ decimal = Math.floor(decimal / 8);
+ }
+ console.log(octal);
+}
+
+// Function to convert binary to hexadecimal
+function binToHex(n) {
+ let decimal = 0;
+ let power = 1;
+ while (n > 0) {
+ const lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n = Math.floor(n / 10);
+ }
+ let hexaDeciNum = [];
+ let i = 0;
+ while (decimal !== 0) {
+ let temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = String.fromCharCode(temp + 48);
+ } else {
+ hexaDeciNum[i] = String.fromCharCode(temp + 55);
+ }
+ i++;
+ decimal = Math.floor(decimal / 16);
+ }
+ console.log(hexaDeciNum.reverse().join(''));
+}
+
+// Function to convert octal to binary
+function octToBin(n) {
+ let decimal = 0;
+ let power = 1;
+ while (n > 0) {
+ const lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n = Math.floor(n / 10);
+ }
+ let binaryNum = [];
+ let i = 0;
+ while (decimal > 0) {
+ binaryNum[i] = decimal % 2;
+ decimal = Math.floor(decimal / 2);
+ i++;
+ }
+ console.log(binaryNum.reverse().join(''));
+}
+
+// Function to convert octal to hexadecimal
+function octToHex(n) {
+ let decimal = 0;
+ let power = 1;
+ while (n > 0) {
+ const lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n = Math.floor(n / 10);
+ }
+ let hexaDeciNum = [];
+ let i = 0;
+ while (decimal !== 0) {
+ let temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = String.fromCharCode(temp + 48);
+ } else {
+ hexaDeciNum[i] = String.fromCharCode(temp + 55);
+ }
+ i++;
+ decimal = Math.floor(decimal / 16);
+ }
+ console.log(hexaDeciNum.reverse().join(''));
+}
+
+// Function to convert hexadecimal to binary
+function hexToBin(hexVal) {
+ let len = hexVal.length;
+ let base = 1;
+ let dec_val = 0;
+ for (let i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal.charCodeAt(i) - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal.charCodeAt(i) - 55) * base;
+ base = base * 16;
+ }
+ }
+ let binaryNum = [];
+ let i = 0;
+ while (dec_val > 0) {
+ binaryNum[i] = dec_val % 2;
+ dec_val = Math.floor(dec_val / 2);
+ i++;
+ }
+ console.log(binaryNum.reverse().join(''));
+}
+
+// Function to convert hexadecimal to octal
+function hexToOct(hexVal) {
+ let len = hexVal.length;
+ let base = 1;
+ let dec_val = 0;
+ for (let i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal.charCodeAt(i) - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal.charCodeAt(i) - 55) * base;
+ base = base * 16;
+ }
+ }
+ let octalNum = [];
+ let i = 0;
+ while (dec_val !== 0) {
+ octalNum[i] = dec_val % 8;
+ dec_val = Math.floor(dec_val / 8);
+ i++;
+ }
+ console.log(octalNum.reverse().join(''));
+}
+
+// Function to convert a number from one base to another
+function convert(num, base1, base2) {
+ let decimal = 0;
+ let power = 1;
+ let len = num.length;
+ for (let i = len - 1; i >= 0; i--) {
+ let digit;
+ if (num[i] >= '0' && num[i] <= '9') {
+ digit = num[i].charCodeAt(0) - 48;
+ } else if (num[i] >= 'A' && num[i] <= 'Z') {
+ digit = num[i].charCodeAt(0) - 65 + 10;
+ } else if (num[i] >= 'a' && num[i] <= 'z') {
+ digit = num[i].charCodeAt(0) - 97 + 10;
+ } else {
+ console.log("Invalid input!");
+ return;
+ }
+ if (digit >= base1) {
+ console.log("Invalid input!");
+ return;
+ }
+ decimal += digit * power;
+ power *= base1;
+ }
+ let result = "";
+ while (decimal > 0) {
+ let digit = decimal % base2;
+ if (digit < 10) {
+ result = String.fromCharCode(digit + 48) + result;
+ } else {
+ result = String.fromCharCode(digit - 10 + 65) + result;
+ }
+ decimal = Math.floor(decimal / base2);
+ }
+ console.log("Result: " + result);
+}
+
+// Example usage of the conversion functions
+let decimalNumber = 42;
+let hexValue = "2A";
+let binaryValue = 11010;
+let octalValue = 52;
+
+decToBin(decimalNumber);
+decToOct(decimalNumber);
+decToHex(decimalNumber);
+binToDec(binaryValue);
+octToDec(octalValue);
+hexToDec(hexValue);
+binToOct(binaryValue);
+binToHex(binaryValue);
+octToBin(octalValue);
+octToHex(octalValue);
+hexToBin(hexValue);
+hexToOct(hexValue);
+
+convert("1A", 16, 2); // Convert hex "1A" to binary
+convert("1101", 2, 10); // Convert binary "1101" to decimal
+convert("57", 8, 16); // Convert octal "57" to hexadecimal
\ No newline at end of file
diff --git a/Number System/Conversion_In_Java.java b/Number System/Conversion_In_Java.java
new file mode 100644
index 00000000..8fe6eff0
--- /dev/null
+++ b/Number System/Conversion_In_Java.java
@@ -0,0 +1,347 @@
+/*
+Author: Sarthak Sharma
+Github: https://github.com/Sarthak950
+Website: https://sarthak950.netlify.app/
+
+This program contains functions to convert a number from one base to another.
+The bases supported are decimal, binary, octal and hexadecimal.
+
+The functions are:
+1. decToBin(int n) - Convert decimal to binary
+2. decToOct(int n) - Convert decimal to octal
+3. decToHex(int n) - Convert decimal to hexadecimal
+4. binToDec(int n) - Convert binary to decimal
+5. octToDec(int n) - Convert octal to decimal
+6. hexToDec(String hexVal) - Convert hexadecimal to decimal
+7. binToOct(int n) - Convert binary to octal
+8. binToHex(int n) - Convert binary to hexadecimal
+9. octToBin(int n) - Convert octal to binary
+10. octToHex(int n) - Convert octal to hexadecimal
+11. hexToBin(String hexVal) - Convert hexadecimal to binary
+12. hexToOct(String hexVal) - Convert hexadecimal to octal
+
+There is a special function:
+* convert(String num, int base1, int base2)
+This function convert any base from one to another
+aka universal converter
+
+*/
+
+
+public class NumberConverter {
+
+ // Function to convert decimal to binary
+ public static void decToBin(int n) {
+ int[] binaryNum = new int[32];
+ int i = 0;
+ while (n > 0) {
+ binaryNum[i] = n % 2;
+ n = n / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(binaryNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert decimal to octal
+ public static void decToOct(int n) {
+ int[] octalNum = new int[100];
+ int i = 0;
+ while (n != 0) {
+ octalNum[i] = n % 8;
+ n = n / 8;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(octalNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert decimal to hexadecimal
+ public static void decToHex(int n) {
+ char[] hexaDeciNum = new char[100];
+ int i = 0;
+ while (n != 0) {
+ int temp = 0;
+ temp = n % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = (char) (temp + 48);
+ i++;
+ } else {
+ hexaDeciNum[i] = (char) (temp + 55);
+ i++;
+ }
+ n = n / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(hexaDeciNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert binary to decimal
+ public static void binToDec(int n) {
+ int decimal = 0;
+ int power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ System.out.println(decimal);
+ }
+
+ // Function to convert octal to decimal
+ public static void octToDec(int n) {
+ int decimal = 0;
+ int power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ System.out.println(decimal);
+ }
+
+ // Function to convert hexadecimal to decimal
+ public static void hexToDec(String hexVal) {
+ int len = hexVal.length();
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') {
+ dec_val += (hexVal.charAt(i) - 48) * base;
+ base = base * 16;
+ } else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ dec_val += (hexVal.charAt(i) - 55) * base;
+ base = base * 16;
+ }
+ }
+ System.out.println(dec_val);
+ }
+
+ // Function to convert binary to octal
+ public static void binToOct(int n) {
+ int octal = 0;
+ int decimal = 0;
+ int power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ power = 1;
+ while (decimal > 0) {
+ int lastDigit = decimal % 8;
+ octal += lastDigit * power;
+ power *= 10;
+ decimal /= 8;
+ }
+ System.out.println(octal);
+ }
+
+ // Function to convert binary to hexadecimal
+ public static void binToHex(int n) {
+ int decimal = 0;
+ int power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ char[] hexaDeciNum = new char[100];
+ int i = 0;
+ while (decimal != 0) {
+ int temp = 0;
+ temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = (char) (temp + 48);
+ i++;
+ } else {
+ hexaDeciNum[i] = (char) (temp + 55);
+ i++;
+ }
+ decimal = decimal / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(hexaDeciNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert octal to binary
+ public static void octToBin(int n) {
+ int decimal = 0;
+ int power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ int[] binaryNum = new int[32];
+ int i = 0;
+ while (decimal > 0) {
+ binaryNum[i] = decimal % 2;
+ decimal = decimal / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(binaryNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert octal to hexadecimal
+ public static void octToHex(int n) {
+ int decimal = 0;
+ int power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ char[] hexaDeciNum = new char[100];
+ int i = 0;
+ while (decimal != 0) {
+ int temp = 0;
+ temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = (char) (temp + 48);
+ i++;
+ } else {
+ hexaDeciNum[i] = (char) (temp + 55);
+ i++;
+ }
+ decimal = decimal / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(hexaDeciNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert hexadecimal to binary
+ public static void hexToBin(String hexVal) {
+ int len = hexVal.length();
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') {
+ dec_val += (hexVal.charAt(i) - 48) * base;
+ base = base * 16;
+ } else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ dec_val += (hexVal.charAt(i) - 55) * base;
+ base = base * 16;
+ }
+ }
+ int[] binaryNum = new int[32];
+ int i = 0;
+ while (dec_val > 0) {
+ binaryNum[i] = dec_val % 2;
+ dec_val = dec_val / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(binaryNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert hexadecimal to octal
+ public static void hexToOct(String hexVal) {
+ int len = hexVal.length();
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') {
+ dec_val += (hexVal.charAt(i) - 48) * base;
+ base = base * 16;
+ } else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ dec_val += (hexVal.charAt(i) - 55) * base;
+ base = base * 16;
+ }
+ }
+ int[] octalNum = new int[100];
+ int i = 0;
+ while (dec_val != 0) {
+ octalNum[i] = dec_val % 8;
+ dec_val = dec_val / 8;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ System.out.print(octalNum[j]);
+ }
+ System.out.println();
+ }
+
+ // Function to convert a number from one base to another
+ public static void convert(String num, int base1, int base2) {
+ int decimal = 0;
+ int power = 1;
+ int len = num.length();
+ for (int i = len - 1; i >= 0; i--) {
+ int digit;
+ if (num.charAt(i) >= '0' && num.charAt(i) <= '9') {
+ digit = num.charAt(i) - '0';
+ } else if (num.charAt(i) >= 'A' && num.charAt(i) <= 'Z') {
+ digit = num.charAt(i) - 'A' + 10;
+ } else if (num.charAt(i) >= 'a' && num.charAt(i) <= 'z') {
+ digit = num.charAt(i) - 'a' + 10;
+ } else {
+ System.out.println("Invalid input!");
+ return;
+ }
+ if (digit >= base1) {
+ System.out.println("Invalid input!");
+ return;
+ }
+ decimal += digit * power;
+ power *= base1;
+ }
+ StringBuilder result = new StringBuilder();
+ while (decimal > 0) {
+ int digit = decimal % base2;
+ if (digit < 10) {
+ result.insert(0, (char) (digit + '0'));
+ } else {
+ result.insert(0, (char) (digit - 10 + 'A'));
+ }
+ decimal = decimal / base2;
+ }
+ System.out.println("Result: " + result);
+ }
+
+ public static void main(String[] args) {
+ // Example usage of the conversion functions
+ int decimalNumber = 42;
+ String hexValue = "2A";
+ int binaryValue = 11010;
+ int octalValue = 52;
+
+ decToBin(decimalNumber);
+ decToOct(decimalNumber);
+ decToHex(decimalNumber);
+ binToDec(binaryValue);
+ octToDec(octalValue);
+ hexToDec(hexValue);
+ binToOct(binaryValue);
+ binToHex(binaryValue);
+ octToBin(octalValue);
+ octToHex(octalValue);
+ hexToBin(hexValue);
+ hexToOct(hexValue);
+
+ convert("1A", 16, 2); // Convert hex "1A" to binary
+ convert("1101", 2, 10); // Convert binary "1101" to decimal
+ convert("57", 8, 16); // Convert octal "57" to hexadecimal
+ }
+}
diff --git a/Number System/Conversion_In_TS.ts b/Number System/Conversion_In_TS.ts
new file mode 100644
index 00000000..103d64c5
--- /dev/null
+++ b/Number System/Conversion_In_TS.ts
@@ -0,0 +1,342 @@
+/*
+Author: Sarthak Sharma
+Github: https://github.com/Sarthak950
+Website: https://sarthak950.netlify.app/
+
+This program contains functions to convert a number from one base to another.
+The bases supported are decimal, binary, octal and hexadecimal.
+
+The functions are:
+1. decToBin(n) - Convert decimal to binary
+2. decToOct(n) - Convert decimal to octal
+3. decToHex(n) - Convert decimal to hexadecimal
+4. binToDec(n) - Convert binary to decimal
+5. octToDec(n) - Convert octal to decimal
+6. hexToDec(hexVal) - Convert hexadecimal to decimal
+7. binToOct(n) - Convert binary to octal
+8. binToHex(n) - Convert binary to hexadecimal
+9. octToBin(n) - Convert octal to binary
+10. octToHex(n) - Convert octal to hexadecimal
+11. hexToBin(hexVal) - Convert hexadecimal to binary
+12. hexToOct(hexVal) - Convert hexadecimal to octal
+
+There is a special function
+* convert(num, base1, base2) - Convert a number from one base to another
+This function converts any number from base1 to base2.
+aka Universal converter
+
+*/
+// Function to convert decimal to binary
+function decToBin(n: number): void {
+ const binaryNum: number[] = [];
+ let i: number = 0;
+ while (n > 0) {
+ binaryNum[i] = n % 2;
+ n = Math.floor(n / 2);
+ i++;
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(binaryNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert decimal to octal
+function decToOct(n: number): void {
+ const octalNum: number[] = [];
+ let i: number = 0;
+ while (n !== 0) {
+ octalNum[i] = n % 8;
+ n = Math.floor(n / 8);
+ i++;
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(octalNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert decimal to hexadecimal
+function decToHex(n: number): void {
+ const hexaDeciNum: string[] = [];
+ let i: number = 0;
+ while (n !== 0) {
+ const temp: number = n % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = String.fromCharCode(temp + 48);
+ } else {
+ hexaDeciNum[i] = String.fromCharCode(temp + 55);
+ }
+ i++;
+ n = Math.floor(n / 16);
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(hexaDeciNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert binary to decimal
+function binToDec(n: number): void {
+ let decimal: number = 0;
+ let power: number = 1;
+ while (n > 0) {
+ const lastDigit: number = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n = Math.floor(n / 10);
+ }
+ console.log(decimal);
+ console.log();
+}
+
+// Function to convert octal to decimal
+function octToDec(n: number): void {
+ let decimal: number = 0;
+ let power: number = 1;
+ while (n > 0) {
+ const lastDigit: number = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n = Math.floor(n / 10);
+ }
+ console.log(decimal);
+ console.log();
+}
+
+// Function to convert hexadecimal to decimal
+function hexToDec(hexVal: string): void {
+ const len: number = hexVal.length;
+ let base: number = 1;
+ let dec_val: number = 0;
+ for (let i = len - 1; i >= 0; i--) {
+ const charCode: number = hexVal.charCodeAt(i);
+ if (charCode >= 48 && charCode <= 57) {
+ dec_val += (charCode - 48) * base;
+ base = base * 16;
+ } else if (charCode >= 65 && charCode <= 70) {
+ dec_val += (charCode - 55) * base;
+ base = base * 16;
+ }
+ }
+ console.log(dec_val);
+ console.log();
+}
+
+// Function to convert binary to octal
+function binToOct(n: number): void {
+ let octal: number = 0;
+ let decimal: number = 0;
+ let power: number = 1;
+ while (n > 0) {
+ const lastDigit: number = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n = Math.floor(n / 10);
+ }
+ power = 1;
+ while (decimal > 0) {
+ const lastDigit: number = decimal % 8;
+ octal += lastDigit * power;
+ power *= 10;
+ decimal = Math.floor(decimal / 8);
+ }
+ console.log(octal);
+ console.log();
+}
+
+// Function to convert binary to hexadecimal
+function binToHex(n: number): void {
+ let decimal: number = 0;
+ let power: number = 1;
+ while (n > 0) {
+ const lastDigit: number = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n = Math.floor(n / 10);
+ }
+ const hexaDeciNum: string[] = [];
+ let i: number = 0;
+ while (decimal !== 0) {
+ const temp: number = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = String.fromCharCode(temp + 48);
+ } else {
+ hexaDeciNum[i] = String.fromCharCode(temp + 55);
+ }
+ i++;
+ decimal = Math.floor(decimal / 16);
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(hexaDeciNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert octal to binary
+function octToBin(n: number): void {
+ let decimal: number = 0;
+ let power: number = 1;
+ while (n > 0) {
+ const lastDigit: number = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n = Math.floor(n / 10);
+ }
+ const binaryNum: number[] = [];
+ let i: number = 0;
+ while (decimal > 0) {
+ binaryNum[i] = decimal % 2;
+ decimal = Math.floor(decimal / 2);
+ i++;
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(binaryNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert octal to hexadecimal
+function octToHex(n: number): void {
+ let decimal: number = 0;
+ let power: number = 1;
+ while (n > 0) {
+ const lastDigit: number = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n = Math.floor(n / 10);
+ }
+ const hexaDeciNum: string[] = [];
+ let i: number = 0;
+ while (decimal !== 0) {
+ const temp: number = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = String.fromCharCode(temp + 48);
+ } else {
+ hexaDeciNum[i] = String.fromCharCode(temp + 55);
+ }
+ i++;
+ decimal = Math.floor(decimal / 16);
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(hexaDeciNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert hexadecimal to binary
+function hexToBin(hexVal: string): void {
+ const len: number = hexVal.length;
+ let base: number = 1;
+ let dec_val: number = 0;
+ for (let i = len - 1; i >= 0; i--) {
+ const charCode: number = hexVal.charCodeAt(i);
+ if (charCode >= 48 && charCode <= 57) {
+ dec_val += (charCode - 48) * base;
+ base = base * 16;
+ } else if (charCode >= 65 && charCode <= 70) {
+ dec_val += (charCode - 55) * base;
+ base = base * 16;
+ }
+ }
+ const binaryNum: number[] = [];
+ let i: number = 0;
+ while (dec_val > 0) {
+ binaryNum[i] = dec_val % 2;
+ dec_val = Math.floor(dec_val / 2);
+ i++;
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(binaryNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert hexadecimal to octal
+function hexToOct(hexVal: string): void {
+ const len: number = hexVal.length;
+ let base: number = 1;
+ let dec_val: number = 0;
+ for (let i = len - 1; i >= 0; i--) {
+ const charCode: number = hexVal.charCodeAt(i);
+ if (charCode >= 48 && charCode <= 57) {
+ dec_val += (charCode - 48) * base;
+ base = base * 16;
+ } else if (charCode >= 65 && charCode <= 70) {
+ dec_val += (charCode - 55) * base;
+ base = base * 16;
+ }
+ }
+ const octalNum: number[] = [];
+ let i: number = 0;
+ while (dec_val !== 0) {
+ octalNum[i] = dec_val % 8;
+ dec_val = Math.floor(dec_val / 8);
+ i++;
+ }
+ for (let j = i - 1; j >= 0; j--) {
+ console.log(octalNum[j]);
+ }
+ console.log();
+}
+
+// Function to convert a number from one base to another
+function convert(num: string, base1: number, base2: number): void {
+ let decimal: number = 0;
+ let power: number = 1;
+ const len: number = num.length;
+ for (let i = len - 1; i >= 0; i--) {
+ const charCode: number = num.charCodeAt(i);
+ let digit: number;
+ if (charCode >= 48 && charCode <= 57) {
+ digit = charCode - 48;
+ } else if (charCode >= 65 && charCode <= 90) {
+ digit = charCode - 65 + 10;
+ } else if (charCode >= 97 && charCode <= 122) {
+ digit = charCode - 97 + 10;
+ } else {
+ console.log("Invalid input!");
+ return;
+ }
+ if (digit >= base1) {
+ console.log("Invalid input!");
+ return;
+ }
+ decimal += digit * power;
+ power *= base1;
+ }
+ let result: string = "";
+ while (decimal > 0) {
+ const digit: number = decimal % base2;
+ if (digit < 10) {
+ result = String.fromCharCode(digit + 48) + result;
+ } else {
+ result = String.fromCharCode(digit - 10 + 65) + result;
+ }
+ decimal = Math.floor(decimal / base2);
+ }
+ console.log("Result: " + result);
+}
+
+// Example usage of the conversion functions
+const decimalNumber: number = 42;
+const hexValue: string = "2A";
+const binaryValue: number = 11010;
+const octalValue: number = 52;
+
+decToBin(decimalNumber);
+decToOct(decimalNumber);
+decToHex(decimalNumber);
+binToDec(binaryValue);
+octToDec(octalValue);
+hexToDec(hexValue);
+binToOct(binaryValue);
+binToHex(binaryValue);
+octToBin(octalValue);
+octToHex(octalValue);
+hexToBin(hexValue);
+hexToOct(hexValue);
+
+convert("1A", 16, 2); // Convert hex "1A" to binary
+convert("1101", 2, 10); // Convert binary "1101" to decimal
+convert("57", 8, 16); // Convert octal "57" to hexadecimal
diff --git a/Number System/Conversion_In_c++.cpp b/Number System/Conversion_In_c++.cpp
new file mode 100644
index 00000000..dbf1226f
--- /dev/null
+++ b/Number System/Conversion_In_c++.cpp
@@ -0,0 +1,341 @@
+/*
+Author: Sarthak Sharma
+Github: https://github.com/Sarthak950
+Website: https://sarthak950.netlify.app/
+
+This program contains functions to convert a number from one base to another.
+The bases supported are decimal, binary, octal and hexadecimal.
+
+The functions are:
+1. decToBin(int n) - Convert decimal to binary
+2. decToOct(int n) - Convert decimal to octal
+3. decToHex(int n) - Convert decimal to hexadecimal
+4. binToDec(int n) - Convert binary to decimal
+5. octToDec(int n) - Convert octal to decimal
+6. hexToDec(string hexVal) - Convert hexadecimal to decimal
+7. binToOct(int n) - Convert binary to octal
+8. binToHex(int n) - Convert binary to hexadecimal
+9. octToBin(int n) - Convert octal to binary
+10. octToHex(int n) - Convert octal to hexadecimal
+11. hexToBin(string hexVal) - Convert hexadecimal to binary
+12. hexToOct(string hexVal) - Convert hexadecimal to octal
+
+There is a special function
+* convert(string num, int base1, int base2)
+which takes a number as a string and converts it from base1 to base2.
+aka Universal converter
+
+*/
+
+
+#include
+#include
+using namespace std;
+
+// Function to convert decimal to binary
+void decToBin(int n) {
+ int binaryNum[32];
+ int i = 0;
+ while (n > 0) {
+ binaryNum[i] = n % 2;
+ n = n / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << binaryNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert decimal to octal
+void decToOct(int n) {
+ int octalNum[100];
+ int i = 0;
+ while (n != 0) {
+ octalNum[i] = n % 8;
+ n = n / 8;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << octalNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert decimal to hexadecimal
+void decToHex(int n) {
+ char hexaDeciNum[100];
+ int i = 0;
+ while (n != 0) {
+ int temp = 0;
+ temp = n % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = temp + 48;
+ i++;
+ } else {
+ hexaDeciNum[i] = temp + 55;
+ i++;
+ }
+ n = n / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << hexaDeciNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert binary to decimal
+void binToDec(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ cout << decimal << endl;
+}
+
+// Function to convert octal to decimal
+void octToDec(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ cout << decimal << endl;
+}
+
+// Function to convert hexadecimal to decimal
+void hexToDec(string hexVal) {
+ int len = hexVal.length();
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal[i] - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal[i] - 55) * base;
+ base = base * 16;
+ }
+ }
+ cout << dec_val << endl;
+}
+
+// Function to convert binary to octal
+void binToOct(int n) {
+ int octal = 0, decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ power = 1;
+ while (decimal > 0) {
+ int lastDigit = decimal % 8;
+ octal += lastDigit * power;
+ power *= 10;
+ decimal /= 8;
+ }
+ cout << octal << endl;
+}
+
+// Function to convert binary to hexadecimal
+void binToHex(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 2;
+ n /= 10;
+ }
+ char hexaDeciNum[100];
+ int i = 0;
+ while (decimal != 0) {
+ int temp = 0;
+ temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = temp + 48;
+ i++;
+ } else {
+ hexaDeciNum[i] = temp + 55;
+ i++;
+ }
+ decimal = decimal / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << hexaDeciNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert octal to binary
+void octToBin(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ int binaryNum[32];
+ int i = 0;
+ while (decimal > 0) {
+ binaryNum[i] = decimal % 2;
+ decimal = decimal / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << binaryNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert octal to hexadecimal
+void octToHex(int n) {
+ int decimal = 0, power = 1;
+ while (n > 0) {
+ int lastDigit = n % 10;
+ decimal += lastDigit * power;
+ power *= 8;
+ n /= 10;
+ }
+ char hexaDeciNum[100];
+ int i = 0;
+ while (decimal != 0) {
+ int temp = 0;
+ temp = decimal % 16;
+ if (temp < 10) {
+ hexaDeciNum[i] = temp + 48;
+ i++;
+ } else {
+ hexaDeciNum[i] = temp + 55;
+ i++;
+ }
+ decimal = decimal / 16;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << hexaDeciNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert hexadecimal to binary
+void hexToBin(string hexVal) {
+ int len = hexVal.length();
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal[i] - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal[i] - 55) * base;
+ base = base * 16;
+ }
+ }
+ int binaryNum[32];
+ int i = 0;
+ while (dec_val > 0) {
+ binaryNum[i] = dec_val % 2;
+ dec_val = dec_val / 2;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << binaryNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert hexadecimal to octal
+void hexToOct(string hexVal) {
+ int len = hexVal.length();
+ int base = 1;
+ int dec_val = 0;
+ for (int i = len - 1; i >= 0; i--) {
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ dec_val += (hexVal[i] - 48) * base;
+ base = base * 16;
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ dec_val += (hexVal[i] - 55) * base;
+ base = base * 16;
+ }
+ }
+ int octalNum[100];
+ int i = 0;
+ while (dec_val != 0) {
+ octalNum[i] = dec_val % 8;
+ dec_val = dec_val / 8;
+ i++;
+ }
+ for (int j = i - 1; j >= 0; j--) {
+ cout << octalNum[j];
+ }
+ cout << endl;
+}
+
+// Function to convert a number from one base to another
+void convert(string num, int base1, int base2) {
+ int decimal = 0, power = 1, len = num.length();
+ for (int i = len - 1; i >= 0; i--) {
+ int digit;
+ if (num[i] >= '0' && num[i] <= '9') {
+ digit = num[i] - '0';
+ } else if (num[i] >= 'A' && num[i] <= 'Z') {
+ digit = num[i] - 'A' + 10;
+ } else if (num[i] >= 'a' && num[i] <= 'z') {
+ digit = num[i] - 'a' + 10;
+ } else {
+ cout << "Invalid input!" << endl;
+ return;
+ }
+ if (digit >= base1) {
+ cout << "Invalid input!" << endl;
+ return;
+ }
+ decimal += digit * power;
+ power *= base1;
+ }
+ string result = "";
+ while (decimal > 0) {
+ int digit = decimal % base2;
+ if (digit < 10) {
+ result = char(digit + '0') + result;
+ } else {
+ result = char(digit - 10 + 'A') + result;
+ }
+ decimal /= base2;
+ }
+ cout << "Result: " << result << endl;
+}
+
+int main() {
+ // Example usage of the conversion functions
+ int decimalNumber = 42;
+ string hexValue = "2A";
+ int binaryValue = 11010;
+ int octalValue = 52;
+
+ decToBin(decimalNumber);
+ decToOct(decimalNumber);
+ decToHex(decimalNumber);
+ binToDec(binaryValue);
+ octToDec(octalValue);
+ hexToDec(hexValue);
+ binToOct(binaryValue);
+ binToHex(binaryValue);
+ octToBin(octalValue);
+ octToHex(octalValue);
+ hexToBin(hexValue);
+ hexToOct(hexValue);
+
+ convert("1A", 16, 2); // Convert hex "1A" to binary
+ convert("1101", 2, 10); // Convert binary "1101" to decimal
+ convert("57", 8, 16); // Convert octal "57" to hexadecimal
+
+ return 0;
+}
diff --git a/Number System/Convesion_In_Lua.lua b/Number System/Convesion_In_Lua.lua
new file mode 100644
index 00000000..e4e45afa
--- /dev/null
+++ b/Number System/Convesion_In_Lua.lua
@@ -0,0 +1,339 @@
+/*
+Author: Sarthak Sharma
+Github: https://github.com/Sarthak950
+Website: https://sarthak950.netlify.app/
+
+This program contains functions to convert a number from one base to another.
+The bases supported are decimal, binary, octal and hexadecimal.
+
+The functions are:
+decToBin(n) - Convert decimal to binary
+decToOct(n) - Convert decimal to octal
+decToHex(n) - Convert decimal to hexadecimal
+binToDec(n) - Convert binary to decimal
+octToDec(n) - Convert octal to decimal
+hexToDec(hexVal) - Convert hexadecimal to decimal
+binToOct(n) - Convert binary to octal
+binToHex(n) - Convert binary to hexadecimal
+octToBin(n) - Convert octal to binary
+octToHex(n) - Convert octal to hexadecimal
+hexToBin(hexVal) - Convert hexadecimal to binary
+hexToOct(hexVal) - Convert hexadecimal to octal
+
+There is also a function to convert a number from one base to another.
+* convert(num, base1, base2) - Convert a number from one base to another
+aka universal converter
+
+*/
+
+
+-- Function to convert decimal to binary
+function decToBin(n)
+ local binaryNum = {}
+ local i = 1
+ while n > 0 do
+ binaryNum[i] = n % 2
+ n = math.floor(n / 2)
+ i = i + 1
+ end
+ for j = #binaryNum, 1, -1 do
+ io.write(binaryNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert decimal to octal
+function decToOct(n)
+ local octalNum = {}
+ local i = 1
+ while n ~= 0 do
+ octalNum[i] = n % 8
+ n = math.floor(n / 8)
+ i = i + 1
+ end
+ for j = #octalNum, 1, -1 do
+ io.write(octalNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert decimal to hexadecimal
+function decToHex(n)
+ local hexaDeciNum = {}
+ local i = 1
+ while n ~= 0 do
+ local temp = n % 16
+ if temp < 10 then
+ hexaDeciNum[i] = string.char(temp + 48)
+ else
+ hexaDeciNum[i] = string.char(temp + 55)
+ end
+ i = i + 1
+ n = math.floor(n / 16)
+ end
+ for j = #hexaDeciNum, 1, -1 do
+ io.write(hexaDeciNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert binary to decimal
+function binToDec(n)
+ local decimal = 0
+ local power = 1
+ while n > 0 do
+ local lastDigit = n % 10
+ decimal = decimal + lastDigit * power
+ power = power * 2
+ n = math.floor(n / 10)
+ end
+ print(decimal)
+end
+
+-- Function to convert octal to decimal
+function octToDec(n)
+ local decimal = 0
+ local power = 1
+ while n > 0 do
+ local lastDigit = n % 10
+ decimal = decimal + lastDigit * power
+ power = power * 8
+ n = math.floor(n / 10)
+ end
+ print(decimal)
+end
+
+-- Function to convert hexadecimal to decimal
+function hexToDec(hexVal)
+ local len = string.len(hexVal)
+ local base = 1
+ local dec_val = 0
+ for i = len, 1, -1 do
+ local charCode = string.byte(hexVal, i)
+ if charCode >= 48 and charCode <= 57 then
+ dec_val = dec_val + (charCode - 48) * base
+ base = base * 16
+ elseif charCode >= 65 and charCode <= 70 then
+ dec_val = dec_val + (charCode - 55) * base
+ base = base * 16
+ end
+ end
+ print(dec_val)
+end
+
+-- Function to convert binary to octal
+function binToOct(n)
+ local octal = 0
+ local decimal = 0
+ local power = 1
+ while n > 0 do
+ local lastDigit = n % 10
+ decimal = decimal + lastDigit * power
+ power = power * 2
+ n = math.floor(n / 10)
+ end
+ power = 1
+ while decimal > 0 do
+ local lastDigit = decimal % 8
+ octal = octal + lastDigit * power
+ power = power * 10
+ decimal = math.floor(decimal / 8)
+ end
+ print(octal)
+end
+
+-- Function to convert binary to hexadecimal
+function binToHex(n)
+ local decimal = 0
+ local power = 1
+ while n > 0 do
+ local lastDigit = n % 10
+ decimal = decimal + lastDigit * power
+ power = power * 2
+ n = math.floor(n / 10)
+ end
+ local hexaDeciNum = {}
+ local i = 1
+ while decimal ~= 0 do
+ local temp = decimal % 16
+ if temp < 10 then
+ hexaDeciNum[i] = string.char(temp + 48)
+ else
+ hexaDeciNum[i] = string.char(temp + 55)
+ end
+ i = i + 1
+ decimal = math.floor(decimal / 16)
+ end
+ for j = #hexaDeciNum, 1, -1 do
+ io.write(hexaDeciNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert octal to binary
+function octToBin(n)
+ local decimal = 0
+ local power = 1
+ while n > 0 do
+ local lastDigit = n % 10
+ decimal = decimal + lastDigit * power
+ power = power * 8
+ n = math.floor(n / 10)
+ end
+ local binaryNum = {}
+ local i = 1
+ while decimal > 0 do
+ binaryNum[i] = decimal % 2
+ decimal = math.floor(decimal / 2)
+ i = i + 1
+ end
+ for j = #binaryNum, 1, -1 do
+ io.write(binaryNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert octal to hexadecimal
+function octToHex(n)
+ local decimal = 0
+ local power = 1
+ while n > 0 do
+ local lastDigit = n % 10
+ decimal = decimal + lastDigit * power
+ power = power * 8
+ n = math.floor(n / 10)
+ end
+ local hexaDeciNum = {}
+ local i = 1
+ while decimal ~= 0 do
+ local temp = decimal % 16
+ if temp < 10 then
+ hexaDeciNum[i] = string.char(temp + 48)
+ else
+ hexaDeciNum[i] = string.char(temp + 55)
+ end
+ i = i + 1
+ decimal = math.floor(decimal / 16)
+ end
+ for j = #hexaDeciNum, 1, -1 do
+ io.write(hexaDeciNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert hexadecimal to binary
+function hexToBin(hexVal)
+ local len = string.len(hexVal)
+ local base = 1
+ local dec_val = 0
+ for i = len, 1, -1 do
+ local charCode = string.byte(hexVal, i)
+ if charCode >= 48 and charCode <= 57 then
+ dec_val = dec_val + (charCode - 48) * base
+ base = base * 16
+ elseif charCode >= 65 and charCode <= 70 then
+ dec_val = dec_val + (charCode - 55) * base
+ base = base * 16
+ end
+ end
+ local binaryNum = {}
+ local i = 1
+ while dec_val > 0 do
+ binaryNum[i] = dec_val % 2
+ dec_val = math.floor(dec_val / 2)
+ i = i + 1
+ end
+ for j = #binaryNum, 1, -1 do
+ io.write(binaryNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert hexadecimal to octal
+function hexToOct(hexVal)
+ local len = string.len(hexVal)
+ local base = 1
+ local dec_val = 0
+ for i = len, 1, -1 do
+ local charCode = string.byte(hexVal, i)
+ if charCode >= 48 and charCode <= 57 then
+ dec_val = dec_val + (charCode - 48) * base
+ base = base * 16
+ elseif charCode >= 65 and charCode <= 70 then
+ dec_val = dec_val + (charCode - 55) * base
+ base = base * 16
+ end
+ end
+ local octalNum = {}
+ local i = 1
+ while dec_val ~= 0 do
+ octalNum[i] = dec_val % 8
+ dec_val = math.floor(dec_val / 8)
+ i = i + 1
+ end
+ for j = #octalNum, 1, -1 do
+ io.write(octalNum[j])
+ end
+ io.write("\n")
+end
+
+-- Function to convert a number from one base to another
+function convert(num, base1, base2)
+ local decimal = 0
+ local power = 1
+ local len = string.len(num)
+ for i = len, 1, -1 do
+ local charCode = string.byte(num, i)
+ local digit
+ if charCode >= 48 and charCode <= 57 then
+ digit = charCode - 48
+ elseif charCode >= 65 and charCode <= 90 then
+ digit = charCode - 65 + 10
+ elseif charCode >= 97 and charCode <= 122 then
+ digit = charCode - 97 + 10
+ else
+ print("Invalid input!")
+ return
+ end
+ if digit >= base1 then
+ print("Invalid input!")
+ return
+ end
+ decimal = decimal + digit * power
+ power = power * base1
+ end
+ local result = ""
+ while decimal > 0 do
+ local digit = decimal % base2
+ if digit < 10 then
+ result = string.char(digit + 48) .. result
+ else
+ result = string.char(digit - 10 + 65) .. result
+ end
+ decimal = math.floor(decimal / base2)
+ end
+ print("Result: " .. result)
+end
+
+-- Example usage of the conversion functions
+local decimalNumber = 42
+local hexValue = "2A"
+local binaryValue = 11010
+local octalValue = 52
+
+decToBin(decimalNumber)
+decToOct(decimalNumber)
+decToHex(decimalNumber)
+binToDec(binaryValue)
+octToDec(octalValue)
+hexToDec(hexValue)
+binToOct(binaryValue)
+binToHex(binaryValue)
+octToBin(octalValue)
+octToHex(octalValue)
+hexToBin(hexValue)
+hexToOct(hexValue)
+
+convert("1A", 16, 2) -- Convert hex "1A" to binary
+convert("1101", 2, 10) -- Convert binary "1101" to decimal
+convert("57", 8, 16) -- Convert octal "57" to hexadecimal
From ac538ac0c1c22c8e0604ab4257109b6682ecb89f Mon Sep 17 00:00:00 2001
From: Sarthak950
Date: Fri, 6 Oct 2023 14:05:56 +0530
Subject: [PATCH 3/6] commnented all the code for the conversion and linked
them into Number System/readme.md
---
Number System/Conversion_In_C.c | 173 +++++++++++++------
Number System/Conversion_In_JS.js | 226 +++++++++++++++++-------
Number System/Conversion_In_Java.java | 238 ++++++++++++++++++++++----
Number System/Conversion_In_TS.ts | 234 ++++++++++++++++++++++---
Number System/Conversion_In_c++.cpp | 238 ++++++++++++++++++++++----
Number System/Convesion_In_Lua.lua | 147 ++++++++++++----
Number System/readme.md | 11 ++
7 files changed, 1032 insertions(+), 235 deletions(-)
diff --git a/Number System/Conversion_In_C.c b/Number System/Conversion_In_C.c
index 6c89a85e..c191626a 100644
--- a/Number System/Conversion_In_C.c
+++ b/Number System/Conversion_In_C.c
@@ -30,30 +30,39 @@ Also this is a special function that convert ant number from one base to another
#include
#include
-// Function to convert decimal to binary
+// function to convert decimal to binary
void decToBin(int n) {
+ // array to store binary number
int binaryNum[32];
+ // counter for binary array
int i = 0;
+ // divide n by 2, store remainder in array, and increment counter
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
+ // print binaryNum array in reverse
for (int j = i - 1; j >= 0; j--) {
printf("%d", binaryNum[j]);
}
+ // print newline
printf("\n");
}
// Function to convert decimal to octal
void decToOct(int n) {
+ // Create an array of integers to hold the octal digits
int octalNum[100];
+ // Create a counter variable to keep track of the current digit
int i = 0;
+ // While n is not equal to 0, keep dividing by 8 and storing the remainder in the array
while (n != 0) {
octalNum[i] = n % 8;
n = n / 8;
i++;
}
+ // Print the digits in reverse order
for (int j = i - 1; j >= 0; j--) {
printf("%d", octalNum[j]);
}
@@ -62,20 +71,31 @@ void decToOct(int n) {
// Function to convert decimal to hexadecimal
void decToHex(int n) {
+ // create a character array to store the hexadecimal number
char hexaDeciNum[100];
+
+ // create a variable to store the index of the array
int i = 0;
+
+ // while the decimal number is not 0, convert it to hexadecimal
while (n != 0) {
+ // create a temporary variable to store the remainder of the division
+ // between the decimal number and 16
int temp = 0;
temp = n % 16;
+ // if the remainder is less than 10, add 48 to it and store it in the array
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
+ // otherwise, add 55 to it and store it in the array
} else {
hexaDeciNum[i] = temp + 55;
i++;
}
+ // divide the decimal number by 16
n = n / 16;
}
+ // loop through the array backwards and print each character
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexaDeciNum[j]);
}
@@ -84,47 +104,67 @@ void decToHex(int n) {
// Function to convert binary to decimal
void binToDec(int n) {
+ // Initialize decimal to 0 and power to 1
int decimal = 0, power = 1;
+ // While n is greater than 0
while (n > 0) {
+ // Get the last digit of n
int lastDigit = n % 10;
+ // Add the last digit times the power to decimal
decimal += lastDigit * power;
+ // Multiply power by 2
power *= 2;
+ // Divide n by 10
n /= 10;
}
+ // Print decimal
printf("%d\n", decimal);
}
// Function to convert octal to decimal
void octToDec(int n) {
+ // Initialize decimal to 0, and power to 1
int decimal = 0, power = 1;
+
+ // While n is greater than 0
while (n > 0) {
+ // Get the last digit of n
int lastDigit = n % 10;
+
+ // Add the last digit times power to decimal
decimal += lastDigit * power;
+
+ // Multiply power by 8
power *= 8;
+
+ // Divide n by 10
n /= 10;
}
+
+ // Print decimal
printf("%d\n", decimal);
}
// Function to convert hexadecimal to decimal
void hexToDec(char *hexVal) {
- int len = strlen(hexVal);
- int base = 1;
- int dec_val = 0;
- for (int i = len - 1; i >= 0; i--) {
- if (hexVal[i] >= '0' && hexVal[i] <= '9') {
- dec_val += (hexVal[i] - 48) * base;
- base = base * 16;
- } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
- dec_val += (hexVal[i] - 55) * base;
- base = base * 16;
+ int len = strlen(hexVal); // get the length of the hex number
+ int base = 1; // set the base value to 1
+ int dec_val = 0; // set the decimal value to 0
+ for (int i = len - 1; i >= 0; i--) { // loop through each digit in the hex number
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') { // if the digit is between 0 and 9
+ dec_val += (hexVal[i] - 48) * base; // add the value of the digit to the decimal value
+ base = base * 16; // multiply the base by 16
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') { // if the digit is between A and F
+ dec_val += (hexVal[i] - 55) * base; // add the value of the digit to the decimal value
+ base = base * 16; // multiply the base by 16
}
}
- printf("%d\n", dec_val);
+ printf("%d\n", dec_val); // print the decimal value
}
// Function to convert binary to octal
void binToOct(int n) {
+ // Convert binary to decimal
int octal = 0, decimal = 0, power = 1;
while (n > 0) {
int lastDigit = n % 10;
@@ -132,6 +172,7 @@ void binToOct(int n) {
power *= 2;
n /= 10;
}
+ // Convert decimal to octal
power = 1;
while (decimal > 0) {
int lastDigit = decimal % 8;
@@ -144,6 +185,7 @@ void binToOct(int n) {
// Function to convert binary to hexadecimal
void binToHex(int n) {
+ // Convert from binary to decimal
int decimal = 0, power = 1;
while (n > 0) {
int lastDigit = n % 10;
@@ -151,6 +193,7 @@ void binToHex(int n) {
power *= 2;
n /= 10;
}
+ // Convert from decimal to hexadecimal
char hexaDeciNum[100];
int i = 0;
while (decimal != 0) {
@@ -165,6 +208,7 @@ void binToHex(int n) {
}
decimal = decimal / 16;
}
+ // Print the hexadecimal number
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexaDeciNum[j]);
}
@@ -173,6 +217,7 @@ void binToHex(int n) {
// Function to convert octal to binary
void octToBin(int n) {
+ // Convert octal to decimal
int decimal = 0, power = 1;
while (n > 0) {
int lastDigit = n % 10;
@@ -180,6 +225,7 @@ void octToBin(int n) {
power *= 8;
n /= 10;
}
+ // Convert decimal to binary
int binaryNum[32];
int i = 0;
while (decimal > 0) {
@@ -187,6 +233,7 @@ void octToBin(int n) {
decimal = decimal / 2;
i++;
}
+ // Print binary
for (int j = i - 1; j >= 0; j--) {
printf("%d", binaryNum[j]);
}
@@ -195,6 +242,7 @@ void octToBin(int n) {
// Function to convert octal to hexadecimal
void octToHex(int n) {
+ // Convert octal to decimal
int decimal = 0, power = 1;
while (n > 0) {
int lastDigit = n % 10;
@@ -202,6 +250,7 @@ void octToHex(int n) {
power *= 8;
n /= 10;
}
+ // Convert decimal to hexadecimal
char hexaDeciNum[100];
int i = 0;
while (decimal != 0) {
@@ -216,6 +265,7 @@ void octToHex(int n) {
}
decimal = decimal / 16;
}
+ // Print hexadecimal equivalent
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexaDeciNum[j]);
}
@@ -224,28 +274,43 @@ void octToHex(int n) {
// Function to convert hexadecimal to binary
void hexToBin(char *hexVal) {
+ // Get the length of the hexadecimal number
int len = strlen(hexVal);
+ // Set the base value to 1
int base = 1;
+ // Set the decimal value to 0
int dec_val = 0;
+ // Iterate through the hexadecimal number
for (int i = len - 1; i >= 0; i--) {
+ // If the current digit is between 0 and 9, add the decimal value to the current digit multiplied by the base
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
dec_val += (hexVal[i] - 48) * base;
base = base * 16;
+ // Else if the current digit is between A and F, add the decimal value to the current digit multiplied by the base
} else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
dec_val += (hexVal[i] - 55) * base;
base = base * 16;
}
}
+ // Create an array to store the binary number
int binaryNum[32];
+ // Set the index to 0
int i = 0;
+ // While the decimal value is greater than 0
while (dec_val > 0) {
+ // Set the current index of the binary number array to the remainder of the decimal value divided by 2
binaryNum[i] = dec_val % 2;
+ // Divide the decimal value by 2
dec_val = dec_val / 2;
+ // Increment the index
i++;
}
+ // Iterate through the binary number array from the end to the beginning
for (int j = i - 1; j >= 0; j--) {
+ // Print the current digit of the binary number
printf("%d", binaryNum[j]);
}
+ // Print a new line
printf("\n");
}
@@ -254,15 +319,20 @@ void hexToOct(char *hexVal) {
int len = strlen(hexVal);
int base = 1;
int dec_val = 0;
+ // loop through each character in hexVal
for (int i = len - 1; i >= 0; i--) {
+ // if the character is a number
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ // convert to decimal and add to dec_val
dec_val += (hexVal[i] - 48) * base;
base = base * 16;
} else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ // convert to decimal and add to dec_val
dec_val += (hexVal[i] - 55) * base;
base = base * 16;
}
}
+ // convert dec_val to octal
int octalNum[100];
int i = 0;
while (dec_val != 0) {
@@ -270,6 +340,7 @@ void hexToOct(char *hexVal) {
dec_val = dec_val / 8;
i++;
}
+ // print octalNum
for (int j = i - 1; j >= 0; j--) {
printf("%d", octalNum[j]);
}
@@ -280,63 +351,63 @@ void hexToOct(char *hexVal) {
// Function to convert a number from one base to another
void convert(char *num, int base1, int base2) {
- int decimal = 0, power = 1, len = strlen(num);
- for (int i = len - 1; i >= 0; i--) {
+ int decimal = 0, power = 1, len = strlen(num); // declare variables
+ for (int i = len - 1; i >= 0; i--) { // loop through each digit of the number
int digit;
- if (num[i] >= '0' && num[i] <= '9') {
- digit = num[i] - '0';
- } else if (num[i] >= 'A' && num[i] <= 'Z') {
- digit = num[i] - 'A' + 10;
- } else if (num[i] >= 'a' && num[i] <= 'z') {
- digit = num[i] - 'a' + 10;
+ if (num[i] >= '0' && num[i] <= '9') { // if digit is from 0 to 9
+ digit = num[i] - '0'; // convert digit to its corresponding decimal value
+ } else if (num[i] >= 'A' && num[i] <= 'Z') { // if digit is from A to Z
+ digit = num[i] - 'A' + 10; // convert digit to its corresponding decimal value
+ } else if (num[i] >= 'a' && num[i] <= 'z') { // if digit is from a to z
+ digit = num[i] - 'a' + 10; // convert digit to its corresponding decimal value
} else {
- printf("Invalid input!\n");
- return -1;
+ printf("Invalid input!\n"); // if digit is not a valid digit, print error message
+ return -1; // exit function
}
- if (digit >= base1) {
+ if (digit >= base1) { // if digit is greater than the base, print error message
printf("Invalid input!\n");
- return -1;
+ return -1; // exit function
}
- decimal += digit * power;
- power *= base1;
+ decimal += digit * power; // add digit to decimal value
+ power *= base1; // multiply power by base
}
- char *result = malloc(sizeof(char) * 100);
+ char *result = malloc(sizeof(char) * 100); // allocate memory for result
int i = 0;
- while (decimal > 0) {
+ while (decimal > 0) { // convert decimal number to base2
int digit = decimal % base2;
if (digit < 10) {
- result[i++] = digit + '0';
+ result[i++] = digit + '0'; // convert digit to its corresponding ASCII value
} else {
- result[i++] = digit - 10 + 'A';
+ result[i++] = digit - 10 + 'A'; // convert digit to its corresponding ASCII value
}
decimal /= base2;
}
result[i] = '\0';
- strrev(result);
- printf("Result: %s\n", result);
- free(result);
- return;
+ strrev(result); // reverse result string
+ printf("Result: %s\n", result); // print result
+ free(result); // free memory
+ return; // exit function
}
int main() {
- // Example usage of the conversion functions
- int decimalNumber = 42;
- char *hexValue = "2A";
- int binaryValue = 11010;
- int octalValue = 52;
+
+ int decimalNumber = 42; // an integer with a decimal value
+ char *hexValue = "2A"; // a string with a hexadecimal value
+ int binaryValue = 11010; // an integer with a binary value
+ int octalValue = 52; // an integer with an octal value
- decToBin(decimalNumber);
- decToOct(decimalNumber);
- decToHex(decimalNumber);
- binToDec(binaryValue);
- octToDec(octalValue);
- hexToDec(hexValue);
- binToOct(binaryValue);
- binToHex(binaryValue);
- octToBin(octalValue);
- octToHex(octalValue);
- hexToBin(hexValue);
- hexToOct(hexValue);
+ decToBin(decimalNumber); // Convert decimal number to binary
+ decToOct(decimalNumber); // Convert decimal number to octal
+ decToHex(decimalNumber); // Convert decimal number to hexadecimal
+ binToDec(binaryValue); // Convert binary number to decimal
+ octToDec(octalValue); // Convert octal number to decimal
+ hexToDec(hexValue); // Convert hexadecimal number to decimal
+ binToOct(binaryValue); // Convert binary number to octal
+ binToHex(binaryValue); // Convert binary number to hexadecimal
+ octToBin(octalValue); // Convert octal number to binary
+ octToHex(octalValue); // Convert octal number to hexadecimal
+ hexToBin(hexValue); // Convert hexadecimal number to binary
+ hexToOct(hexValue); // Convert hexadecimal number to octal
convert("1A", 16, 2); // Convert hex "1A" to binary
convert("1101", 2, 10); // Convert binary "1101" to decimal
diff --git a/Number System/Conversion_In_JS.js b/Number System/Conversion_In_JS.js
index cc11c682..d7ca25d6 100644
--- a/Number System/Conversion_In_JS.js
+++ b/Number System/Conversion_In_JS.js
@@ -29,53 +29,76 @@ aka Universal converter
// Function to convert decimal to binary
function decToBin(n) {
- let binaryNum = [];
- let i = 0;
- while (n > 0) {
- binaryNum[i] = n % 2;
- n = Math.floor(n / 2);
- i++;
+ let binaryNum = []; //create an empty array to hold the binary number
+ let i = 0; //create a counter variable to keep track of the array index
+ while (n > 0) { //while the decimal number is greater than zero
+ binaryNum[i] = n % 2; //store the remainder of n/2 in the array (0 or 1)
+ n = Math.floor(n / 2); //divide n by 2 and round down
+ i++; //increase the array index counter by 1
}
- console.log(binaryNum.reverse().join(''));
+ console.log(binaryNum.reverse().join('')); //print the binary number after reversing it and converting to a string
}
// Function to convert decimal to octal
function decToOct(n) {
+ // Create an empty array to store the octal digits
let octalNum = [];
+ // Create a counter variable i to keep track of the index of the octalNum array
let i = 0;
+ // Until n becomes 0, do the following:
while (n !== 0) {
+ // Store the remainder of n / 8 in the ith index of the octalNum array
octalNum[i] = n % 8;
+ // Update n to be the quotient of n / 8
n = Math.floor(n / 8);
+ // Increment i by 1
i++;
}
+ // Print out the octalNum array in reverse order
console.log(octalNum.reverse().join(''));
}
// Function to convert decimal to hexadecimal
function decToHex(n) {
+ // Create an empty array to store the converted hexadecimal number
let hexaDeciNum = [];
+ // Create a variable to keep track of the index of the array
let i = 0;
+ // Create a while loop that runs as long as the number is not zero
while (n !== 0) {
+ // Create a temporary variable to hold the remainder of n divided by 16
let temp = n % 16;
+ // If the remainder is less than 10, convert it to a character
if (temp < 10) {
hexaDeciNum[i] = String.fromCharCode(temp + 48);
} else {
+ // If the remainder is greater than 10, convert it to a letter
hexaDeciNum[i] = String.fromCharCode(temp + 55);
}
+ // Increase the index variable by 1
i++;
+ // Divide n by 16 and round down to the nearest whole number
n = Math.floor(n / 16);
}
+ // Print the hexadecimal number
console.log(hexaDeciNum.reverse().join(''));
}
// Function to convert binary to decimal
function binToDec(n) {
+ // create a variable to store the decimal value
let decimal = 0;
+ // create a variable to store the value of the digit's place (1, 10, 100, etc.)
let power = 1;
+ // loop through the binary number
while (n > 0) {
+ // get the last digit
const lastDigit = n % 10;
+ // add the last digit's value to the decimal value
decimal += lastDigit * power;
+ // multiply the power by 2
power *= 2;
+ // remove the last digit from the binary number
n = Math.floor(n / 10);
}
console.log(decimal);
@@ -83,50 +106,71 @@ function binToDec(n) {
// Function to convert octal to decimal
function octToDec(n) {
- let decimal = 0;
- let power = 1;
+ let decimal = 0; // create a variable to store the decimal number
+ let power = 1; // multiply the last digit by 1
while (n > 0) {
- const lastDigit = n % 10;
- decimal += lastDigit * power;
- power *= 8;
- n = Math.floor(n / 10);
+ const lastDigit = n % 10; // get the last digit of the octal number
+ decimal += lastDigit * power; // add the last digit multiplied by the power to the decimal number
+ power *= 8; // increase the power by multiplying it by 8
+ n = Math.floor(n / 10); // remove the last digit from the octal number
}
- console.log(decimal);
+ console.log(decimal); // print the decimal number
}
// Function to convert hexadecimal to decimal
function hexToDec(hexVal) {
+ // Length of the hexadecimal number
let len = hexVal.length;
+ // Initialising base value to 1, i.e 16^0
let base = 1;
+ // Initialising decimal value to 0
let dec_val = 0;
+ // Extracting characters as digits from last character
for (let i = len - 1; i >= 0; i--) {
+ // If the character lies in the range '0'-'9', convert it to integral 0-9
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
dec_val += (hexVal.charCodeAt(i) - 48) * base;
base = base * 16;
- } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ }
+ // If the character lies in the range 'A'-'F', convert it to integral 10-15
+ else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
dec_val += (hexVal.charCodeAt(i) - 55) * base;
base = base * 16;
}
}
+ // Printing the decimal equivalent
console.log(dec_val);
}
// Function to convert binary to octal
function binToOct(n) {
+ // Initialize octal number and decimal number
let octal = 0;
let decimal = 0;
+ // Initialize power of 2
let power = 1;
+ // Convert binary to decimal
while (n > 0) {
+ // Extract last digit
const lastDigit = n % 10;
+ // Update decimal number
decimal += lastDigit * power;
+ // Update power of 2
power *= 2;
+ // Update binary number
n = Math.floor(n / 10);
}
+ // Initialize power of 10
power = 1;
+ // Convert decimal to octal
while (decimal > 0) {
+ // Extract last digit
const lastDigit = decimal % 8;
+ // Update octal number
octal += lastDigit * power;
+ // Update power of 10
power *= 10;
+ // Update decimal number
decimal = Math.floor(decimal / 8);
}
console.log(octal);
@@ -134,21 +178,42 @@ function binToOct(n) {
// Function to convert binary to hexadecimal
function binToHex(n) {
+ // Convert decimal to binary
let decimal = 0;
let power = 1;
+
+ // Go through each digit of the binary number
while (n > 0) {
+ // Get the last digit
const lastDigit = n % 10;
+
+ // Add it to the decimal value
decimal += lastDigit * power;
+
+ // Increase the power of 2
power *= 2;
+
+ // Remove the last digit
n = Math.floor(n / 10);
}
+
+ // Return the decimal value
+ return decimal;
+
+ // Convert binary to hexadecimal
let hexaDeciNum = [];
let i = 0;
while (decimal !== 0) {
let temp = decimal % 16;
if (temp < 10) {
+ // temp is a number
+ // Convert temp to a string and add 48 to get the ASCII code for the
+ // character 0-9
hexaDeciNum[i] = String.fromCharCode(temp + 48);
} else {
+ // temp is a letter
+ // Convert temp to a string and add 55 to get the ASCII code for
+ // the character A-F
hexaDeciNum[i] = String.fromCharCode(temp + 55);
}
i++;
@@ -159,19 +224,24 @@ function binToHex(n) {
// Function to convert octal to binary
function octToBin(n) {
+ // Convert octal number to decimal
let decimal = 0;
let power = 1;
while (n > 0) {
- const lastDigit = n % 10;
- decimal += lastDigit * power;
- power *= 8;
- n = Math.floor(n / 10);
+ const lastDigit = n % 10; // lastDigit = 5
+ decimal += lastDigit * power; // decimal = 5 * 1 = 5
+ power *= 8; // power = 8
+ n = Math.floor(n / 10); // n = 1
}
+ // Convert decimal number to binary
let binaryNum = [];
let i = 0;
while (decimal > 0) {
+ // Initialize the array to hold the binary number
binaryNum[i] = decimal % 2;
+ // Divide the decimal number by 2
decimal = Math.floor(decimal / 2);
+ // Increment the index
i++;
}
console.log(binaryNum.reverse().join(''));
@@ -179,24 +249,36 @@ function octToBin(n) {
// Function to convert octal to hexadecimal
function octToHex(n) {
+ // Convert octal to decimal
let decimal = 0;
let power = 1;
while (n > 0) {
+ // Extract the last digit.
const lastDigit = n % 10;
+
+ // Convert the last digit from octal to decimal.
decimal += lastDigit * power;
+
+ // Increase the power of 8.
power *= 8;
+
+ // Remove the last digit.
n = Math.floor(n / 10);
}
+ // Convert decimal to hexadecimal
let hexaDeciNum = [];
let i = 0;
while (decimal !== 0) {
+ // find the remainder
let temp = decimal % 16;
+ // convert to hex digit
if (temp < 10) {
hexaDeciNum[i] = String.fromCharCode(temp + 48);
} else {
hexaDeciNum[i] = String.fromCharCode(temp + 55);
}
i++;
+ // divide by 16
decimal = Math.floor(decimal / 16);
}
console.log(hexaDeciNum.reverse().join(''));
@@ -204,44 +286,55 @@ function octToHex(n) {
// Function to convert hexadecimal to binary
function hexToBin(hexVal) {
- let len = hexVal.length;
- let base = 1;
- let dec_val = 0;
- for (let i = len - 1; i >= 0; i--) {
- if (hexVal[i] >= '0' && hexVal[i] <= '9') {
- dec_val += (hexVal.charCodeAt(i) - 48) * base;
- base = base * 16;
- } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
- dec_val += (hexVal.charCodeAt(i) - 55) * base;
- base = base * 16;
+ let len = hexVal.length; // stores the length of the input string
+
+ let base = 1; // stores the base value to be used in converting the hex to decimal
+ let dec_val = 0; // stores the decimal value of the input string
+
+ for (let i = len - 1; i >= 0; i--) { // loops through each character in the string from right to left
+ if (hexVal[i] >= '0' && hexVal[i] <= '9') { // checks if the character is a digit
+ dec_val += (hexVal.charCodeAt(i) - 48) * base; // adds the decimal value of the digit to the current decimal value
+ base = base * 16; // multiplies the base value by 16 to be used in the next iteration
+ } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') { // checks if the character is a letter
+ dec_val += (hexVal.charCodeAt(i) - 55) * base; // adds the decimal value of the letter to the current decimal value
+ base = base * 16; // multiplies the base value by 16 to be used in the next iteration
}
}
- let binaryNum = [];
- let i = 0;
- while (dec_val > 0) {
- binaryNum[i] = dec_val % 2;
- dec_val = Math.floor(dec_val / 2);
- i++;
+ let binaryNum = []; // stores the binary value of the decimal value
+ let i = 0; // stores the index of the binaryNum array
+ while (dec_val > 0) { // loops until the decimal value is 0
+ binaryNum[i] = dec_val % 2; // stores the remainder of the decimal value divided by 2
+ dec_val = Math.floor(dec_val / 2); // divides the decimal value by 2 and rounds down
+ i++; // increments the index of the binaryNum array
}
console.log(binaryNum.reverse().join(''));
}
// Function to convert hexadecimal to octal
function hexToOct(hexVal) {
+ // The length of the hexVal string is stored in the len variable
let len = hexVal.length;
+ // The base variable is set to 1
let base = 1;
+ // The dec_val variable is set to 0
let dec_val = 0;
+ // A for loop is used to iterate through each character in the hexVal string
for (let i = len - 1; i >= 0; i--) {
+ // If the character is a number, the dec_val variable is updated with the decimal value of the hex value
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
dec_val += (hexVal.charCodeAt(i) - 48) * base;
base = base * 16;
+ // If the character is a letter, the dec_val variable is updated with the decimal value of the hex value
} else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
dec_val += (hexVal.charCodeAt(i) - 55) * base;
base = base * 16;
}
}
+ // The octalNum array is declared
let octalNum = [];
+ // The i variable is set to 0
let i = 0;
+ // A while loop is used to convert the decimal value to octal
while (dec_val !== 0) {
octalNum[i] = dec_val % 8;
dec_val = Math.floor(dec_val / 8);
@@ -252,37 +345,48 @@ function hexToOct(hexVal) {
// Function to convert a number from one base to another
function convert(num, base1, base2) {
- let decimal = 0;
- let power = 1;
- let len = num.length;
- for (let i = len - 1; i >= 0; i--) {
- let digit;
- if (num[i] >= '0' && num[i] <= '9') {
- digit = num[i].charCodeAt(0) - 48;
- } else if (num[i] >= 'A' && num[i] <= 'Z') {
- digit = num[i].charCodeAt(0) - 65 + 10;
- } else if (num[i] >= 'a' && num[i] <= 'z') {
- digit = num[i].charCodeAt(0) - 97 + 10;
- } else {
- console.log("Invalid input!");
- return;
+ let decimal = 0; // This will be the final decimal value.
+ let power = 1; // This will be the power of the current digit.
+ let len = num.length; // This is the length of the input number.
+ for (let i = len - 1; i >= 0; i--) { // Iterate through the input number in reverse order.
+ let digit; // This will be the decimal value of the current digit.
+ if (num[i] >= '0' && num[i] <= '9') { // If the current digit is a number...
+ digit = num[i].charCodeAt(0) - 48; // Convert the digit to its decimal value.
+ } else if (num[i] >= 'A' && num[i] <= 'Z') { // If the current digit is a capital letter...
+ digit = num[i].charCodeAt(0) - 65 + 10; // Convert the digit to its decimal value.
+ } else if (num[i] >= 'a' && num[i] <= 'z') { // If the current digit is a lowercase letter...
+ digit = num[i].charCodeAt(0) - 97 + 10; // Convert the digit to its decimal value.
+ } else { // If the current digit is not a number or letter...
+ console.log("Invalid input!"); // Print an error message.
+ return; // Exit the function.
}
- if (digit >= base1) {
- console.log("Invalid input!");
- return;
+ if (digit >= base1) { // If the current digit is greater than or equal to the input base...
+ console.log("Invalid input!"); // Print an error message.
+ return; // Exit the function.
}
- decimal += digit * power;
- power *= base1;
+ decimal += digit * power; // Add the decimal value of the current digit to the final decimal value.
+ power *= base1; // Increase the power of the current digit.
}
+ // Convert the decimal number to the given base.
let result = "";
- while (decimal > 0) {
- let digit = decimal % base2;
- if (digit < 10) {
- result = String.fromCharCode(digit + 48) + result;
- } else {
- result = String.fromCharCode(digit - 10 + 65) + result;
+ // Convert the decimal number to the indicated base.
+ // The result is a string of digits, including leading zeros.
+ // The base argument must be between 2 and 36.
+ function numberToString(n, base) {
+ let result = ""; // A string to hold the result.
+ let sign = "";
+ if (n < 0) { // Handle negative numbers.
+ sign = "-";
+ n = -n;
}
- decimal = Math.floor(decimal / base2);
+ // Convert the number to the indicated base.
+ // Repeatedly divide by the base and determine the remainder.
+ // This remainder is the next most significant digit.
+ do {
+ result = String(n % base) + result;
+ n = Math.floor(n / base);
+ } while (n > 0);
+ return sign + result;
}
console.log("Result: " + result);
}
diff --git a/Number System/Conversion_In_Java.java b/Number System/Conversion_In_Java.java
index 8fe6eff0..b80073d5 100644
--- a/Number System/Conversion_In_Java.java
+++ b/Number System/Conversion_In_Java.java
@@ -32,50 +32,77 @@ public class NumberConverter {
// Function to convert decimal to binary
public static void decToBin(int n) {
+ // define an array of int with 32 elements
int[] binaryNum = new int[32];
+ // define an int variable i and set it to 0
int i = 0;
+ // while n is greater than 0
while (n > 0) {
+ // set the current element of binaryNum to the remainder of n / 2
binaryNum[i] = n % 2;
+ // divide n by 2 (integer division)
n = n / 2;
+ // increment i by 1
i++;
}
+ // for each element in binaryNum starting at the end
for (int j = i - 1; j >= 0; j--) {
+ // print the current element in binaryNum
System.out.print(binaryNum[j]);
}
+ // print a new line
System.out.println();
}
// Function to convert decimal to octal
public static void decToOct(int n) {
+ // create an array of ints to hold the octal digits
int[] octalNum = new int[100];
+ // create a variable to hold the number of octal digits
int i = 0;
+ // while n is not equal to zero, do the following
while (n != 0) {
+ // set the ith element of the octalNum array to the remainder of n / 8
octalNum[i] = n % 8;
+ // set n to n / 8
n = n / 8;
+ // increment i
i++;
}
+ // for each element in the octalNum array, starting from the last one
for (int j = i - 1; j >= 0; j--) {
+ // print the jth element in the array
System.out.print(octalNum[j]);
}
+ // print a new line
System.out.println();
}
// Function to convert decimal to hexadecimal
public static void decToHex(int n) {
+ // Initialize a char array to store the hexadecimal digits
char[] hexaDeciNum = new char[100];
+ // Initialize a variable to keep track of the position in the array
int i = 0;
+ // Repeatedly divide the decimal number by 16 and store the remainder in the array
while (n != 0) {
+ // Initialize a variable to store the remainder
int temp = 0;
+ // Find the remainder of the number divided by 16
temp = n % 16;
+ // If the remainder is less than 10, store the remainder as a character in the array
if (temp < 10) {
hexaDeciNum[i] = (char) (temp + 48);
i++;
} else {
+ // If the remainder is greater than 10, store the remainder as a character in the array
hexaDeciNum[i] = (char) (temp + 55);
i++;
}
+ // Divide the number by 16
n = n / 16;
}
+ // Print each character in the array in reverse order
for (int j = i - 1; j >= 0; j--) {
System.out.print(hexaDeciNum[j]);
}
@@ -84,14 +111,22 @@ public static void decToHex(int n) {
// Function to convert binary to decimal
public static void binToDec(int n) {
+ // initialize decimal to 0
int decimal = 0;
+ // initialize power to 1
int power = 1;
+ // repeat until n is 0
while (n > 0) {
+ // get the last digit
int lastDigit = n % 10;
+ // add lastDigit times power to decimal
decimal += lastDigit * power;
+ // multiply power by 2
power *= 2;
+ // divide n by 10
n /= 10;
}
+ // print decimal
System.out.println(decimal);
}
@@ -100,9 +135,13 @@ public static void octToDec(int n) {
int decimal = 0;
int power = 1;
while (n > 0) {
+ // Get the last digit of n
int lastDigit = n % 10;
+ // Add lastDigit * power to decimal
decimal += lastDigit * power;
+ // Multiply power by 8
power *= 8;
+ // Remove the last digit from n
n /= 10;
}
System.out.println(decimal);
@@ -110,66 +149,116 @@ public static void octToDec(int n) {
// Function to convert hexadecimal to decimal
public static void hexToDec(String hexVal) {
+ // Get length of the hexadecimal string
int len = hexVal.length();
+ // Initialize the base value for the hexadecimal number
int base = 1;
+ // Initialize the decimal value
int dec_val = 0;
+ // Iterate from the end of the string to the beginning
for (int i = len - 1; i >= 0; i--) {
+ // If the character at the current index is a digit
if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') {
+ // Add the value of the current digit to the decimal value
dec_val += (hexVal.charAt(i) - 48) * base;
+ // Multiply the base value by 16
base = base * 16;
- } else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ }
+ // If the character at the current index is a letter
+ else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ // Add the value of the current digit to the decimal value
dec_val += (hexVal.charAt(i) - 55) * base;
+ // Multiply the base value by 16
base = base * 16;
}
}
+ // Print the decimal value
System.out.println(dec_val);
}
// Function to convert binary to octal
public static void binToOct(int n) {
int octal = 0;
+ // Convert binary to decimal
int decimal = 0;
int power = 1;
+ // Repeat until there are no more digits to process
while (n > 0) {
+
+ // Get the last digit
int lastDigit = n % 10;
+
+ // Add the last digit to the decimal number
decimal += lastDigit * power;
+
+ // Move to the next power of 2
power *= 2;
+
+ // Remove the last digit
n /= 10;
}
+ // Convert decimal to octal
power = 1;
- while (decimal > 0) {
- int lastDigit = decimal % 8;
- octal += lastDigit * power;
- power *= 10;
- decimal /= 8;
+ while (decimal > 0) {// keep going until decimal is 0
+ int lastDigit = decimal % 8; // get the last digit of decimal
+ octal += lastDigit * power; // add last digit times power to octal
+ power *= 10; // increase the power by a factor of 10
+ decimal /= 8; // divide decimal by 8
}
System.out.println(octal);
}
// Function to convert binary to hexadecimal
public static void binToHex(int n) {
+ // convert binary to decimal
+
+ // Set up a variable to hold the decimal value
int decimal = 0;
+
+ // Set up a variable to hold the power of 2. This starts at 1.
int power = 1;
+
+ // Loop through the binary number
while (n > 0) {
+ // Get the last digit of the binary number
int lastDigit = n % 10;
+
+ // Add the value of the last digit to the decimal value
decimal += lastDigit * power;
+
+ // Multiply the power of 2 by 2
power *= 2;
+
+ // Remove the last digit of the binary number
n /= 10;
}
+ // Convert decimal to hexadecimal
char[] hexaDeciNum = new char[100];
int i = 0;
+ // while the decimal number is not zero
while (decimal != 0) {
+ // define a temporary variable
int temp = 0;
+ // get the remainder of the decimal number divided by 16
temp = decimal % 16;
+ // if the remainder is less than 10
if (temp < 10) {
+ // add 48 to get the ASCII value of the remainder
+ // and store it in the array
hexaDeciNum[i] = (char) (temp + 48);
+ // increment the index variable
i++;
} else {
+ // add 55 to get the ASCII value of the remainder
+ // and store it in the array
hexaDeciNum[i] = (char) (temp + 55);
+ // increment the index variable
i++;
}
+ // divide the decimal number by 16
decimal = decimal / 16;
}
+ // Print the hexadecimal number
for (int j = i - 1; j >= 0; j--) {
System.out.print(hexaDeciNum[j]);
}
@@ -178,21 +267,44 @@ public static void binToHex(int n) {
// Function to convert octal to binary
public static void octToBin(int n) {
+ // Convert octal to decimal
+ int decimal = 0;
+ int power = 1;
+ // Decimal number
int decimal = 0;
+
+ // Power of 8
int power = 1;
+
+ // Iterate through each digit
while (n > 0) {
+ // Get the last digit
int lastDigit = n % 10;
+
+ // Add the digit to the decimal number
decimal += lastDigit * power;
+
+ // Multiply the power by 8
power *= 8;
+
+ // Remove the last digit
n /= 10;
}
+ // Convert decimal to binary
+ // Create an array of 32 integers. Initialize them to 0.
int[] binaryNum = new int[32];
+ // Create a variable to keep track of the index of the array.
int i = 0;
+ // Convert the decimal number to binary.
while (decimal > 0) {
+ // Get the current binary digit.
binaryNum[i] = decimal % 2;
+ // Remove the current binary digit from the number.
decimal = decimal / 2;
+ // Increment the index of the array.
i++;
}
+ // Print out binary number
for (int j = i - 1; j >= 0; j--) {
System.out.print(binaryNum[j]);
}
@@ -201,26 +313,46 @@ public static void octToBin(int n) {
// Function to convert octal to hexadecimal
public static void octToHex(int n) {
+ // convert octal to decimal
+ // Initialize decimal number to 0
int decimal = 0;
+ // Initialize power of 8 to 1
int power = 1;
+ // Loop through each digit
while (n > 0) {
+ // Get the last digit
int lastDigit = n % 10;
+ // Add last digit to decimal
decimal += lastDigit * power;
+ // Multiply power by 8
power *= 8;
+ // Remove the last digit from n
n /= 10;
}
+ // convert decimal to hexadecimal
+ // hexadecimal number
char[] hexaDeciNum = new char[100];
+
+ // counter for hexadecimal number array
int i = 0;
- while (decimal != 0) {
+ // While decimal value is not zero
+while (decimal != 0) {
+ // Create a temporary variable to store the remainder of the decimal value divided by 16
int temp = 0;
temp = decimal % 16;
+
+ // If the remainder is less than 10, the character value is the remainder + 48
if (temp < 10) {
hexaDeciNum[i] = (char) (temp + 48);
i++;
- } else {
+ }
+ // Otherwise, the character value is the remainder + 55
+ else {
hexaDeciNum[i] = (char) (temp + 55);
i++;
}
+
+ // Divide the decimal value by 16
decimal = decimal / 16;
}
for (int j = i - 1; j >= 0; j--) {
@@ -231,64 +363,106 @@ public static void octToHex(int n) {
// Function to convert hexadecimal to binary
public static void hexToBin(String hexVal) {
+ // Get the length of the hexadecimal number
int len = hexVal.length();
+ // Set the base to 1
int base = 1;
+ // Set the decimal value to 0
int dec_val = 0;
+ // Loop through the hexadecimal number
for (int i = len - 1; i >= 0; i--) {
+ // If the character is a number
if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') {
+ // Calculate the decimal value
dec_val += (hexVal.charAt(i) - 48) * base;
+ // Multiple the base by 16
base = base * 16;
+ // If the character is a letter
} else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ // Calculate the decimal value
dec_val += (hexVal.charAt(i) - 55) * base;
+ // Multiple the base by 16
base = base * 16;
}
}
+ // Create an array to store the binary number
int[] binaryNum = new int[32];
+ // Set the index to 0
int i = 0;
+ // While the decimal value is greater than 0
while (dec_val > 0) {
+ // Store the remainder of the decimal value divided by 2 in the array
binaryNum[i] = dec_val % 2;
+ // Divide the decimal value by 2
dec_val = dec_val / 2;
+ // Increment the index
i++;
}
+ // Loop through the array
for (int j = i - 1; j >= 0; j--) {
+ // Print the binary number
System.out.print(binaryNum[j]);
}
+ // Print a new line
System.out.println();
}
// Function to convert hexadecimal to octal
public static void hexToOct(String hexVal) {
+ // Get the length of the string
int len = hexVal.length();
+ // Set the base to 1
int base = 1;
+ // Set the decimal value to 0
int dec_val = 0;
+ // Iterate through the string from the end
for (int i = len - 1; i >= 0; i--) {
+ // Check if the character is a number
if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') {
+ // Add the digit to the decimal value
dec_val += (hexVal.charAt(i) - 48) * base;
+ // Increment the base by 16
base = base * 16;
} else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') {
+ // Add the digit to the decimal value
dec_val += (hexVal.charAt(i) - 55) * base;
+ // Increment the base by 16
base = base * 16;
}
}
+ // Create an array to store the octal digits
int[] octalNum = new int[100];
+ // Set the index to 0
int i = 0;
+ // While the decimal value is not 0
while (dec_val != 0) {
+ // Add the remainder of the decimal value divided by 8 to the array
octalNum[i] = dec_val % 8;
+ // Divide the decimal value by 8
dec_val = dec_val / 8;
+ // Increment the index
i++;
}
+ // Iterate through the array from the end
for (int j = i - 1; j >= 0; j--) {
+ // Print the octal digits
System.out.print(octalNum[j]);
}
+ // Print a new line
System.out.println();
}
// Function to convert a number from one base to another
public static void convert(String num, int base1, int base2) {
- int decimal = 0;
- int power = 1;
- int len = num.length();
+ // Convert the input number from the first base to a decimal number
+ // Convert a string of digits to an integer
+ int decimal = 0; // the integer value of the string
+ int power = 1; // the power of 10 for the current digit
+ int len = num.length(); // the number of digits in the string
+
+ // Convert to decimal
for (int i = len - 1; i >= 0; i--) {
+ // Get the current digit
int digit;
if (num.charAt(i) >= '0' && num.charAt(i) <= '9') {
digit = num.charAt(i) - '0';
@@ -300,45 +474,49 @@ public static void convert(String num, int base1, int base2) {
System.out.println("Invalid input!");
return;
}
+ // Check that the digit is valid
if (digit >= base1) {
System.out.println("Invalid input!");
return;
}
+ // Add the digit to the decimal value
decimal += digit * power;
+ // Calculate the next power of the base
power *= base1;
}
+ // Convert the decimal number to the second base
StringBuilder result = new StringBuilder();
while (decimal > 0) {
- int digit = decimal % base2;
+ int digit = decimal % base2; // Get the rightmost digit
if (digit < 10) {
result.insert(0, (char) (digit + '0'));
} else {
result.insert(0, (char) (digit - 10 + 'A'));
}
- decimal = decimal / base2;
+ decimal = decimal / base2; // Remove the rightmost digit
}
System.out.println("Result: " + result);
}
public static void main(String[] args) {
// Example usage of the conversion functions
- int decimalNumber = 42;
- String hexValue = "2A";
- int binaryValue = 11010;
- int octalValue = 52;
-
- decToBin(decimalNumber);
- decToOct(decimalNumber);
- decToHex(decimalNumber);
- binToDec(binaryValue);
- octToDec(octalValue);
- hexToDec(hexValue);
- binToOct(binaryValue);
- binToHex(binaryValue);
- octToBin(octalValue);
- octToHex(octalValue);
- hexToBin(hexValue);
- hexToOct(hexValue);
+ int decimalNumber = 42; // A decimal number
+ String hexValue = "2A"; // A hexadecimal number
+ int binaryValue = 11010; // A binary number
+ int octalValue = 52; // An octal number
+
+ decToBin(decimalNumber); //Convert decimal to binary.
+ decToOct(decimalNumber); //Convert decimal to octal.
+ decToHex(decimalNumber); //Convert decimal to hexadecimal.
+ binToDec(binaryValue); //Convert binary to decimal.
+ octToDec(octalValue); //Convert octal to decimal.
+ hexToDec(hexValue); //Convert hexadecimal to decimal.
+ binToOct(binaryValue); //Convert binary to octal.
+ binToHex(binaryValue); //Convert binary to hexadecimal.
+ octToBin(octalValue); //Convert octal to binary.
+ octToHex(octalValue); //Convert octal to hexadecimal.
+ hexToBin(hexValue); //Convert hexadecimal to binary.
+ hexToOct(hexValue); //Convert hexadecimal to octal.
convert("1A", 16, 2); // Convert hex "1A" to binary
convert("1101", 2, 10); // Convert binary "1101" to decimal
diff --git a/Number System/Conversion_In_TS.ts b/Number System/Conversion_In_TS.ts
index 103d64c5..01dd8e2d 100644
--- a/Number System/Conversion_In_TS.ts
+++ b/Number System/Conversion_In_TS.ts
@@ -28,28 +28,43 @@ aka Universal converter
*/
// Function to convert decimal to binary
function decToBin(n: number): void {
+ //Create an empty array to store the binary digits
const binaryNum: number[] = [];
+ //Initialize a counter to keep track of the binary digits
let i: number = 0;
+ //While the decimal number is greater than 0, divide by 2 and store the remainder (0 or 1) in the array
while (n > 0) {
binaryNum[i] = n % 2;
n = Math.floor(n / 2);
i++;
}
+ //Loop through the array backwards to print the binary digits
for (let j = i - 1; j >= 0; j--) {
console.log(binaryNum[j]);
}
+ //Add a line break after printing the binary number
console.log();
}
// Function to convert decimal to octal
function decToOct(n: number): void {
+ // create array to store octal number
const octalNum: number[] = [];
+
+ // counter for octal number array
let i: number = 0;
+
+ // dividing the number by 8 until it is 0
while (n !== 0) {
+ // storing remainder in octal array
octalNum[i] = n % 8;
+ // dividing the number by 8
n = Math.floor(n / 8);
+ // increase counter
i++;
}
+
+ // printing octal number array in reverse order
for (let j = i - 1; j >= 0; j--) {
console.log(octalNum[j]);
}
@@ -60,46 +75,63 @@ function decToOct(n: number): void {
function decToHex(n: number): void {
const hexaDeciNum: string[] = [];
let i: number = 0;
+ // While n is not equal to zero
while (n !== 0) {
+ // Find the remainder of n divided by 16
const temp: number = n % 16;
+ // If the remainder is less than 10
if (temp < 10) {
+ // Store the character representation of the remainder
+ // plus 48 in the array
hexaDeciNum[i] = String.fromCharCode(temp + 48);
} else {
+ // Store the character representation of the remainder
+ // plus 55 in the array
hexaDeciNum[i] = String.fromCharCode(temp + 55);
}
+ // Increment i
i++;
+ // Divide n by 16 and round down
n = Math.floor(n / 16);
}
+ // Loop through the array in reverse order
for (let j = i - 1; j >= 0; j--) {
+ // Print the value at index j
console.log(hexaDeciNum[j]);
}
+ // Print a new line
console.log();
}
// Function to convert binary to decimal
function binToDec(n: number): void {
- let decimal: number = 0;
- let power: number = 1;
+ let decimal: number = 0; // The decimal number we are building up
+ let power: number = 1; // The current power of 2 we are using
while (n > 0) {
- const lastDigit: number = n % 10;
- decimal += lastDigit * power;
- power *= 2;
- n = Math.floor(n / 10);
+ const lastDigit: number = n % 10; // Get the last digit of n
+ decimal += lastDigit * power; // Add last digit to decimal
+ power *= 2; // Increase power of 2
+ n = Math.floor(n / 10); // Remove last digit from n
}
- console.log(decimal);
- console.log();
+ console.log(decimal); // Print out the decimal number
+ console.log(); // Print a blank line
}
// Function to convert octal to decimal
function octToDec(n: number): void {
+ // Initialize decimal number and power
let decimal: number = 0;
let power: number = 1;
+
+ // Extracting last digit and adding to decimal
while (n > 0) {
const lastDigit: number = n % 10;
decimal += lastDigit * power;
power *= 8;
n = Math.floor(n / 10);
}
+
+ // Printing result
console.log(decimal);
console.log();
}
@@ -110,11 +142,16 @@ function hexToDec(hexVal: string): void {
let base: number = 1;
let dec_val: number = 0;
for (let i = len - 1; i >= 0; i--) {
+ // Get the character code of the current hex character
const charCode: number = hexVal.charCodeAt(i);
+ // If the character code is between '0' and '9', convert it to decimal
if (charCode >= 48 && charCode <= 57) {
+ // Add to the decimal value by multiplying the character code by the base
dec_val += (charCode - 48) * base;
base = base * 16;
+ // If the character code is between 'A' and 'F', convert it to decimal
} else if (charCode >= 65 && charCode <= 70) {
+ // Add to the decimal value by multiplying the character code by the base
dec_val += (charCode - 55) * base;
base = base * 16;
}
@@ -125,49 +162,92 @@ function hexToDec(hexVal: string): void {
// Function to convert binary to octal
function binToOct(n: number): void {
+ // Initialize octal number
let octal: number = 0;
+
+ // Initialize decimal number
let decimal: number = 0;
+
+ // Initialize power
let power: number = 1;
+
+ // Convert binary to decimal
while (n > 0) {
+ // Extract the last digit of binary number
const lastDigit: number = n % 10;
+
+ // Update decimal number
decimal += lastDigit * power;
+
+ // Update power
power *= 2;
+
+ // Remove last digit of binary number
n = Math.floor(n / 10);
}
+
+ // Convert decimal to octal
power = 1;
while (decimal > 0) {
+ // Extract the last digit of decimal number
const lastDigit: number = decimal % 8;
+
+ // Update octal number
octal += lastDigit * power;
+
+ // Update power
power *= 10;
+
+ // Remove last digit of decimal number
decimal = Math.floor(decimal / 8);
}
+
+ // Print the octal number
console.log(octal);
console.log();
}
// Function to convert binary to hexadecimal
function binToHex(n: number): void {
+ // Initialize decimal number to zero
let decimal: number = 0;
+ // Initialize power to 1
let power: number = 1;
+ // Loop through each digit of the binary number
while (n > 0) {
+ // Get the last digit of the binary number
const lastDigit: number = n % 10;
+ // Add the last digit multiplied by the power to the decimal number
decimal += lastDigit * power;
+ // Multiply the power by 2
power *= 2;
+ // Remove the last digit from the binary number
n = Math.floor(n / 10);
}
+ // Initialize array to store hexadecimal digits
const hexaDeciNum: string[] = [];
+ // Initialize index to zero
let i: number = 0;
+ // Loop through each digit of the decimal number
while (decimal !== 0) {
+ // Get the remainder of dividing the decimal number by 16
const temp: number = decimal % 16;
+ // If remainder is less than 10
if (temp < 10) {
+ // Store the remainder plus 48 in the array
hexaDeciNum[i] = String.fromCharCode(temp + 48);
} else {
+ // Store the remainder plus 55 in the array
hexaDeciNum[i] = String.fromCharCode(temp + 55);
}
+ // Increment index
i++;
+ // Remove the last digit from the decimal number
decimal = Math.floor(decimal / 16);
}
+ // Loop backward through the array
for (let j = i - 1; j >= 0; j--) {
+ // Print the hexadecimal digit
console.log(hexaDeciNum[j]);
}
console.log();
@@ -175,78 +255,134 @@ function binToHex(n: number): void {
// Function to convert octal to binary
function octToBin(n: number): void {
+ // Initialize decimal to 0 and power to 1
let decimal: number = 0;
let power: number = 1;
+ // While n is greater than 0:
while (n > 0) {
+ // Set lastDigit to the last digit of n
const lastDigit: number = n % 10;
+ // Add lastDigit * power to decimal
decimal += lastDigit * power;
+ // Multiply power by 8
power *= 8;
+ // Set n to n / 10
n = Math.floor(n / 10);
}
+ // Initialize binaryNum to an empty array
const binaryNum: number[] = [];
+ // Initialize i to 0
let i: number = 0;
+ // While decimal > 0:
while (decimal > 0) {
+ // Set binaryNum[i] to decimal % 2
binaryNum[i] = decimal % 2;
+ // Set decimal to decimal / 2
decimal = Math.floor(decimal / 2);
+ // Increment i
i++;
}
+ // For j from i - 1 down to 0:
for (let j = i - 1; j >= 0; j--) {
+ // Print binaryNum[j]
console.log(binaryNum[j]);
}
+ // Print a newline
console.log();
}
// Function to convert octal to hexadecimal
function octToHex(n: number): void {
+ // Declare a variable to store the decimal value
let decimal: number = 0;
+ // Declare a variable to store the power value
let power: number = 1;
+ // While the octal number is greater than 0:
while (n > 0) {
+ // Calculate the last digit of the octal number
const lastDigit: number = n % 10;
+ // Add the last digit to the decimal value
decimal += lastDigit * power;
+ // Multiply the power by 8
power *= 8;
+ // Remove the last digit from the octal number
n = Math.floor(n / 10);
}
+ // Declare an array to store the hexadecimal number
const hexaDeciNum: string[] = [];
+ // Declare a variable to store the index of the array
let i: number = 0;
+ // While the decimal number is greater than 0:
while (decimal !== 0) {
+ // Calculate the last digit of the decimal number
const temp: number = decimal % 16;
+ // If the last digit is less than 10:
if (temp < 10) {
+ // Store the last digit in the array
hexaDeciNum[i] = String.fromCharCode(temp + 48);
} else {
+ // Store the last digit in the array
hexaDeciNum[i] = String.fromCharCode(temp + 55);
}
+ // Increment the index
i++;
+ // Remove the last digit from the decimal number
decimal = Math.floor(decimal / 16);
}
+ // For each digit in the array:
for (let j = i - 1; j >= 0; j--) {
+ // Print the digit
console.log(hexaDeciNum[j]);
}
+ // Print a blank line
console.log();
}
// Function to convert hexadecimal to binary
function hexToBin(hexVal: string): void {
+ // Get the length of the input string
const len: number = hexVal.length;
+ // Set the base value to 1
let base: number = 1;
+ // Set the decimal value to 0
let dec_val: number = 0;
+
+ // Loop through the input string from right to left
for (let i = len - 1; i >= 0; i--) {
+ // Get the character code of the current character
const charCode: number = hexVal.charCodeAt(i);
+ // If the character code is between 48 and 57, it is a number
if (charCode >= 48 && charCode <= 57) {
+ // Add the value of the number to the decimal value
dec_val += (charCode - 48) * base;
+ // Multiply the base value by 16
base = base * 16;
+ // If the character code is between 65 and 70, it is a letter
} else if (charCode >= 65 && charCode <= 70) {
+ // Add the value of the letter to the decimal value
dec_val += (charCode - 55) * base;
+ // Multiply the base value by 16
base = base * 16;
}
}
+
+ // Create an array to hold the binary digits
const binaryNum: number[] = [];
+ // Set the index to 0
let i: number = 0;
+ // Loop until the decimal value is 0
while (dec_val > 0) {
+ // Get the remainder when the decimal value is divided by 2
binaryNum[i] = dec_val % 2;
+ // Divide the decimal value by 2
dec_val = Math.floor(dec_val / 2);
+ // Increment the index
i++;
}
+
+ // Loop through the binary digits
for (let j = i - 1; j >= 0; j--) {
+ // Print the binary digit
console.log(binaryNum[j]);
}
console.log();
@@ -254,26 +390,62 @@ function hexToBin(hexVal: string): void {
// Function to convert hexadecimal to octal
function hexToOct(hexVal: string): void {
+
+ // Get the length of the hex value
const len: number = hexVal.length;
+
+ // Set the base to 1
let base: number = 1;
+
+ // Set the decimal value to 0
let dec_val: number = 0;
+
+ // Loop through each character of the hex value
for (let i = len - 1; i >= 0; i--) {
+
+ // Get the numeric value of the character
const charCode: number = hexVal.charCodeAt(i);
+
+ // If the character is a digit
if (charCode >= 48 && charCode <= 57) {
+
+ // Add the digit to the decimal value
dec_val += (charCode - 48) * base;
+
+ // Multiply the base by 16
base = base * 16;
+
+ // If the character is a letter
} else if (charCode >= 65 && charCode <= 70) {
+
+ // Add the digit to the decimal value
dec_val += (charCode - 55) * base;
+
+ // Multiply the base by 16
base = base * 16;
}
}
+
+ // Create an array to store the octal values
const octalNum: number[] = [];
+
+ // Set the index to 0
let i: number = 0;
+
+ // Loop through each digit and convert to octal
while (dec_val !== 0) {
+
+ // Get the remainder
octalNum[i] = dec_val % 8;
+
+ // Divide the decimal value by 8
dec_val = Math.floor(dec_val / 8);
+
+ // Increment the index
i++;
}
+
+ // Print out the octal value
for (let j = i - 1; j >= 0; j--) {
console.log(octalNum[j]);
}
@@ -282,38 +454,38 @@ function hexToOct(hexVal: string): void {
// Function to convert a number from one base to another
function convert(num: string, base1: number, base2: number): void {
- let decimal: number = 0;
- let power: number = 1;
- const len: number = num.length;
+ let decimal: number = 0; // Decimal value of the number in base1
+ let power: number = 1; // Power of base1
+ const len: number = num.length; // Length of the number
for (let i = len - 1; i >= 0; i--) {
- const charCode: number = num.charCodeAt(i);
- let digit: number;
- if (charCode >= 48 && charCode <= 57) {
+ const charCode: number = num.charCodeAt(i); // Character code of the digit
+ let digit: number; // Value of the digit
+ if (charCode >= 48 && charCode <= 57) { // Digit is between 0 and 9
digit = charCode - 48;
- } else if (charCode >= 65 && charCode <= 90) {
+ } else if (charCode >= 65 && charCode <= 90) { // Digit is between A and Z
digit = charCode - 65 + 10;
- } else if (charCode >= 97 && charCode <= 122) {
+ } else if (charCode >= 97 && charCode <= 122) { // Digit is between a and z
digit = charCode - 97 + 10;
- } else {
+ } else { // Invalid input
console.log("Invalid input!");
return;
}
- if (digit >= base1) {
+ if (digit >= base1) { // Digit is not valid in base1
console.log("Invalid input!");
return;
}
- decimal += digit * power;
- power *= base1;
+ decimal += digit * power; // Add the digit to decimal
+ power *= base1; // Multiply the power of base1 by base1
}
- let result: string = "";
+ let result: string = ""; // Result
while (decimal > 0) {
- const digit: number = decimal % base2;
- if (digit < 10) {
+ const digit: number = decimal % base2; // Digit in base2
+ if (digit < 10) { // Digit is between 0 and 9
result = String.fromCharCode(digit + 48) + result;
- } else {
+ } else { // Digit is between A and Z
result = String.fromCharCode(digit - 10 + 65) + result;
}
- decimal = Math.floor(decimal / base2);
+ decimal = Math.floor(decimal / base2); // Divide decimal by base2
}
console.log("Result: " + result);
}
@@ -324,17 +496,29 @@ const hexValue: string = "2A";
const binaryValue: number = 11010;
const octalValue: number = 52;
+// Convert decimal to binary
decToBin(decimalNumber);
+// Convert decimal to octal
decToOct(decimalNumber);
+// Convert decimal to hexadecimal
decToHex(decimalNumber);
+// Convert binary to decimal
binToDec(binaryValue);
+// Convert octal to decimal
octToDec(octalValue);
+// Convert hexadecimal to decimal
hexToDec(hexValue);
+// Convert binary to octal
binToOct(binaryValue);
+// Convert binary to hexadecimal
binToHex(binaryValue);
+// Convert octal to binary
octToBin(octalValue);
+// Convert octal to hexadecimal
octToHex(octalValue);
+// Convert hexadecimal to binary
hexToBin(hexValue);
+// Convert hexadecimal to octal
hexToOct(hexValue);
convert("1A", 16, 2); // Convert hex "1A" to binary
diff --git a/Number System/Conversion_In_c++.cpp b/Number System/Conversion_In_c++.cpp
index dbf1226f..4b0d283c 100644
--- a/Number System/Conversion_In_c++.cpp
+++ b/Number System/Conversion_In_c++.cpp
@@ -34,77 +34,115 @@ using namespace std;
// Function to convert decimal to binary
void decToBin(int n) {
- int binaryNum[32];
- int i = 0;
- while (n > 0) {
- binaryNum[i] = n % 2;
- n = n / 2;
- i++;
+ int binaryNum[32]; // array to store the binary number
+ int i = 0; // counter to keep track of the index
+ while (n > 0) { // while the number is greater than 0
+ binaryNum[i] = n % 2; // store the remainder of dividing n by 2
+ n = n / 2; // update n to be the quotient of dividing n by 2
+ i++; // increment i
}
- for (int j = i - 1; j >= 0; j--) {
- cout << binaryNum[j];
+ for (int j = i - 1; j >= 0; j--) { // loop through the binary number array backwards
+ cout << binaryNum[j]; // print each digit
}
- cout << endl;
+ cout << endl; // print a newline
}
// Function to convert decimal to octal
-void decToOct(int n) {
- int octalNum[100];
- int i = 0;
- while (n != 0) {
- octalNum[i] = n % 8;
- n = n / 8;
- i++;
+void decToOct(int n) { // function declaration
+ int octalNum[100]; // array to store octal number
+ int i = 0; // counter for octal number array
+ while (n != 0) { // while loop to get octal number
+ octalNum[i] = n % 8; // get remainder
+ n = n / 8; // get quotient
+ i++; // increment counter
}
- for (int j = i - 1; j >= 0; j--) {
- cout << octalNum[j];
+ for (int j = i - 1; j >= 0; j--) { // for loop to print octal number
+ cout << octalNum[j]; // print octal number
}
- cout << endl;
+ cout << endl; // print new line
}
// Function to convert decimal to hexadecimal
void decToHex(int n) {
+ // Creates a character array to store the hexadecimal number
char hexaDeciNum[100];
+ // Creates a counter for the array
int i = 0;
+ // While loop to check if n is not equal to 0
while (n != 0) {
+ // Creates a temporary integer variable
int temp = 0;
+ // Sets the value of temp to be the remainder of n divided by 16
temp = n % 16;
+ // If statement to check if temp is less than 10
if (temp < 10) {
+ // Sets the value of hexaDeciNum at index i to be temp + 48
hexaDeciNum[i] = temp + 48;
+ // Increment i by 1
i++;
} else {
+ // Sets the value of hexaDeciNum at index i to be temp + 55
hexaDeciNum[i] = temp + 55;
+ // Increment i by 1
i++;
}
+ // Divide n by 16
n = n / 16;
}
+ // For loop to iterate through the array
for (int j = i - 1; j >= 0; j--) {
+ // Print the value at index j in the array
cout << hexaDeciNum[j];
}
+ // Print a new line
cout << endl;
}
// Function to convert binary to decimal
void binToDec(int n) {
+ // Initialize decimal and power variables
int decimal = 0, power = 1;
+
+ // While n is not 0
while (n > 0) {
+ // Get last digit of n
int lastDigit = n % 10;
+
+ // Add last digit times power to decimal
decimal += lastDigit * power;
+
+ // Update power
power *= 2;
+
+ // Update n
n /= 10;
}
+
+ // Print decimal
cout << decimal << endl;
}
// Function to convert octal to decimal
void octToDec(int n) {
+ // Initialize decimal and power variables
int decimal = 0, power = 1;
+
+ // While n is not 0
while (n > 0) {
+ // Get last digit of n
int lastDigit = n % 10;
+
+ // Add last digit times power to decimal
decimal += lastDigit * power;
+
+ // Update power
power *= 8;
+
+ // Update n
n /= 10;
}
+
+ // Print decimal
cout << decimal << endl;
}
@@ -114,10 +152,16 @@ void hexToDec(string hexVal) {
int base = 1;
int dec_val = 0;
for (int i = len - 1; i >= 0; i--) {
+ // if character lies in '0'-'9', converting
+ // it to integral 0-9 by subtracting 48 from
+ // ASCII value.
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
dec_val += (hexVal[i] - 48) * base;
base = base * 16;
} else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ // if character lies in 'A'-'F' , converting
+ // it to integral 10 - 15 by subtracting 55
+ // from ASCII value
dec_val += (hexVal[i] - 55) * base;
base = base * 16;
}
@@ -125,46 +169,84 @@ void hexToDec(string hexVal) {
cout << dec_val << endl;
}
+#include
+using namespace std;
+
// Function to convert binary to octal
void binToOct(int n) {
int octal = 0, decimal = 0, power = 1;
+ // convert binary to decimal
while (n > 0) {
+ // get last digit of binary
int lastDigit = n % 10;
+ // add last digit times power of 2 to decimal
decimal += lastDigit * power;
+ // increase power of 2
power *= 2;
+ // remove last digit from binary
n /= 10;
}
+ // convert decimal to octal
power = 1;
while (decimal > 0) {
+ // get last digit of decimal
int lastDigit = decimal % 8;
+ // add last digit times power of 10 to octal
octal += lastDigit * power;
+ // increase power of 10
power *= 10;
+ // remove last digit from decimal
decimal /= 8;
}
+ // print octal number
cout << octal << endl;
}
// Function to convert binary to hexadecimal
void binToHex(int n) {
+ // Convert binary to decimal
int decimal = 0, power = 1;
+ // Initialize decimal number to 0
+ int decimal = 0;
+
+ // Initialize power to 1
+ int power = 1;
+
+ // While n is greater than 0 do the following:
while (n > 0) {
+ // Get the last digit of n
int lastDigit = n % 10;
+
+ // Add the last digit times the power to the decimal number
decimal += lastDigit * power;
+
+ // Multiply power by 2
power *= 2;
+
+ // Divide n by 10
n /= 10;
}
+
+ // Convert decimal to hexadecimal
char hexaDeciNum[100];
+ // start with the first digit in the rightmost position
int i = 0;
+ // while the decimal number is not zero
while (decimal != 0) {
+ // create a temporary variable to store the remainder
int temp = 0;
+ // divide the decimal number by 16 and store the remainder
temp = decimal % 16;
+ // if the remainder is less than 10, add 48 to the remainder and store it in the array
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
+ // otherwise, add 55 to the remainder and store it in the array
} else {
hexaDeciNum[i] = temp + 55;
i++;
}
+ // divide the decimal number by 16 to get the next remainder
decimal = decimal / 16;
}
for (int j = i - 1; j >= 0; j--) {
@@ -175,20 +257,43 @@ void binToHex(int n) {
// Function to convert octal to binary
void octToBin(int n) {
+ // Initialize variables
int decimal = 0, power = 1;
+ // Convert octal to decimal
+ // The final decimal number.
+ int decimal = 0;
+ // The power of 8 that we are currently at.
+ int power = 1;
+ // While there are still digits in the octal number.
while (n > 0) {
+ // Get the last digit.
int lastDigit = n % 10;
+ // Add the last digit to the decimal number.
decimal += lastDigit * power;
+ // Move to the next power of 8.
power *= 8;
+ // Remove the last digit.
n /= 10;
}
+ // Convert decimal to binary
int binaryNum[32];
int i = 0;
+ // Initialize a counter to track the array index
+ i = 0;
+
+ // While the decimal number is greater than 0
while (decimal > 0) {
+
+ // Calculate the remainder of decimal / 2
binaryNum[i] = decimal % 2;
+
+ // Calculate the new value of decimal by dividing it by 2
decimal = decimal / 2;
+
+ // Increment the counter
i++;
}
+ // Print binary number
for (int j = i - 1; j >= 0; j--) {
cout << binaryNum[j];
}
@@ -197,27 +302,34 @@ void octToBin(int n) {
// Function to convert octal to hexadecimal
void octToHex(int n) {
+ // Declare and initialize variables
int decimal = 0, power = 1;
- while (n > 0) {
- int lastDigit = n % 10;
- decimal += lastDigit * power;
- power *= 8;
- n /= 10;
+
+ // Convert octal to decimal
+ while (n > 0) { // While there are still digits to process
+ int lastDigit = n % 10; // Extract the last digit
+ decimal += lastDigit * power; // Add to the decimal number
+ power *= 8; // Increase the power of 8
+ n /= 10; // Remove the last digit
}
+
+ // Convert decimal to hexadecimal
char hexaDeciNum[100];
int i = 0;
while (decimal != 0) {
int temp = 0;
- temp = decimal % 16;
+ temp = decimal % 16; // Get the remainder
if (temp < 10) {
- hexaDeciNum[i] = temp + 48;
+ hexaDeciNum[i] = temp + 48; // 48 is ASCII for 0
i++;
} else {
- hexaDeciNum[i] = temp + 55;
+ hexaDeciNum[i] = temp + 55; // 55 is ASCII for A
i++;
}
- decimal = decimal / 16;
+ decimal = decimal / 16; // Get the quotient
}
+
+ // Print hexadecimal number
for (int j = i - 1; j >= 0; j--) {
cout << hexaDeciNum[j];
}
@@ -226,62 +338,97 @@ void octToHex(int n) {
// Function to convert hexadecimal to binary
void hexToBin(string hexVal) {
+ // Get the length of the string
int len = hexVal.length();
+ // Base value for the hexadecimal
int base = 1;
+ // Decimal value
int dec_val = 0;
+ // Iterate through the string
for (int i = len - 1; i >= 0; i--) {
+ // Check if the character is a number
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
+ // Calculate the decimal value
dec_val += (hexVal[i] - 48) * base;
+ // Increment the base
base = base * 16;
} else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ // Calculate the decimal value
dec_val += (hexVal[i] - 55) * base;
+ // Increment the base
base = base * 16;
}
}
+ // Create an array to store the binary value
int binaryNum[32];
+ // Iterate through the decimal value
int i = 0;
while (dec_val > 0) {
+ // Add the remainder to the binary array
binaryNum[i] = dec_val % 2;
+ // Divide the decimal value by 2
dec_val = dec_val / 2;
+ // Increment the index
i++;
}
+ // Iterate through the binary array
for (int j = i - 1; j >= 0; j--) {
+ // Print the binary value
cout << binaryNum[j];
}
+ // Print a new line
cout << endl;
}
// Function to convert hexadecimal to octal
void hexToOct(string hexVal) {
+ // Get the length of the hexadecimal number
int len = hexVal.length();
+ // Initialize the base to 1
int base = 1;
+ // Initialize the decimal value to 0
int dec_val = 0;
+ // Loop through the hexadecimal number from right to left
for (int i = len - 1; i >= 0; i--) {
+ // If the current digit is between 0 and 9, add it to the decimal value
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
dec_val += (hexVal[i] - 48) * base;
base = base * 16;
- } else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
+ }
+ // If the current digit is between A and F, add it to the decimal value
+ else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
dec_val += (hexVal[i] - 55) * base;
base = base * 16;
}
}
+ // Declare an array to store the octal number
int octalNum[100];
+ // Initialize the index to 0
int i = 0;
+ // Loop until the decimal value is 0
while (dec_val != 0) {
+ // Store the remainder when the decimal value is divided by 8
octalNum[i] = dec_val % 8;
+ // Update the decimal value
dec_val = dec_val / 8;
+ // Increment the index
i++;
}
+ // Loop through the octal number in reverse
for (int j = i - 1; j >= 0; j--) {
+ // Print the octal number
cout << octalNum[j];
}
+ // Print a new line
cout << endl;
}
// Function to convert a number from one base to another
void convert(string num, int base1, int base2) {
+ // Convert the number from base1 to base 10
int decimal = 0, power = 1, len = num.length();
for (int i = len - 1; i >= 0; i--) {
+ // Convert the current digit from a character to an int.
int digit;
if (num[i] >= '0' && num[i] <= '9') {
digit = num[i] - '0';
@@ -293,21 +440,34 @@ void convert(string num, int base1, int base2) {
cout << "Invalid input!" << endl;
return;
}
+ // If the digit is greater than the base, it's invalid.
if (digit >= base1) {
cout << "Invalid input!" << endl;
return;
}
+ // Add the contribution of the current digit to the total.
decimal += digit * power;
+ // Update the power of the base.
power *= base1;
}
+ // Convert the number from base 10 to base2
+ // STEP 1: Initialize result string to empty string
string result = "";
+
+ // STEP 2: Repeat until decimal value is 0
while (decimal > 0) {
+ // STEP 2a: Get last digit of decimal value
int digit = decimal % base2;
+
+ // STEP 2b: If digit is less than 10, convert to decimal digit,
+ // otherwise convert to capital letter
if (digit < 10) {
result = char(digit + '0') + result;
} else {
result = char(digit - 10 + 'A') + result;
}
+
+ // STEP 2c: Divide decimal value by base2
decimal /= base2;
}
cout << "Result: " << result << endl;
@@ -315,22 +475,34 @@ void convert(string num, int base1, int base2) {
int main() {
// Example usage of the conversion functions
- int decimalNumber = 42;
- string hexValue = "2A";
- int binaryValue = 11010;
- int octalValue = 52;
+ int decimalNumber = 42; // 42 in decimal
+ string hexValue = "2A"; // 42 in hexadecimal
+ int binaryValue = 11010; // 42 in binary
+ int octalValue = 52; // 42 in octal
+ // add comments here
decToBin(decimalNumber);
+ // add comments here
decToOct(decimalNumber);
+ // add comments here
decToHex(decimalNumber);
+ // add comments here
binToDec(binaryValue);
+ // add comments here
octToDec(octalValue);
+ // add comments here
hexToDec(hexValue);
+ // add comments here
binToOct(binaryValue);
+ // add comments here
binToHex(binaryValue);
+ // add comments here
octToBin(octalValue);
+ // add comments here
octToHex(octalValue);
+ // add comments here
hexToBin(hexValue);
+ // add comments here
hexToOct(hexValue);
convert("1A", 16, 2); // Convert hex "1A" to binary
diff --git a/Number System/Convesion_In_Lua.lua b/Number System/Convesion_In_Lua.lua
index e4e45afa..59b46b3e 100644
--- a/Number System/Convesion_In_Lua.lua
+++ b/Number System/Convesion_In_Lua.lua
@@ -29,16 +29,23 @@ aka universal converter
-- Function to convert decimal to binary
function decToBin(n)
+ -- Create a table to hold the binary number
local binaryNum = {}
+ -- Loop through the steps of the algorithm
local i = 1
while n > 0 do
+ -- Get the remainder of n/2 and add it to the table
binaryNum[i] = n % 2
+ -- Divide n by 2
n = math.floor(n / 2)
+ -- Increment i
i = i + 1
end
+ -- Loop through the table backwards and print each element
for j = #binaryNum, 1, -1 do
io.write(binaryNum[j])
end
+ -- Add a newline at the end
io.write("\n")
end
@@ -46,11 +53,14 @@ end
function decToOct(n)
local octalNum = {}
local i = 1
+ -- This loop divides the input number by 8 until it is 0.
+ -- The remainder of each division is stored in an array.
while n ~= 0 do
octalNum[i] = n % 8
n = math.floor(n / 8)
i = i + 1
end
+ -- This loop prints the octal number in reverse order.
for j = #octalNum, 1, -1 do
io.write(octalNum[j])
end
@@ -59,18 +69,22 @@ end
-- Function to convert decimal to hexadecimal
function decToHex(n)
- local hexaDeciNum = {}
- local i = 1
+ local hexaDeciNum = {} -- Create an empty table to store the hexadecimal number
+ local i = 1 -- Set the index to 1
+ -- Loop until n is 0
while n ~= 0 do
- local temp = n % 16
+ local temp = n % 16 -- Find the remainder of n / 16
+ -- If the remainder is less than 10, store the ASCII value of the remainder plus 48 in the table
if temp < 10 then
hexaDeciNum[i] = string.char(temp + 48)
+ -- Otherwise, store the ASCII value of the remainder plus 55 in the table
else
hexaDeciNum[i] = string.char(temp + 55)
end
- i = i + 1
- n = math.floor(n / 16)
+ i = i + 1 -- Increment the index by 1
+ n = math.floor(n / 16) -- Divide n by 16 and round down
end
+ -- Loop through the table in reverse order and print the hexadecimal number
for j = #hexaDeciNum, 1, -1 do
io.write(hexaDeciNum[j])
end
@@ -81,43 +95,72 @@ end
function binToDec(n)
local decimal = 0
local power = 1
+
+ -- Loop over the binary digits
while n > 0 do
- local lastDigit = n % 10
- decimal = decimal + lastDigit * power
- power = power * 2
- n = math.floor(n / 10)
+ -- n is the binary number to convert
+ -- decimal is the decimal number being created
+ -- power is the current power of 2
+ local n = 101101
+ local decimal = 0
+ local power = 1
+ -- repeat until all digits of n have been processed
+ while n > 0 do
+ -- extract the last digit from n
+ local lastDigit = n % 10
+ -- add the digit to decimal
+ decimal = decimal + lastDigit * power
+ -- update power of 2
+ power = power * 2
+ -- remove the last digit from n
+ n = math.floor(n / 10)
+ end
end
+
+ -- Return the decimal value
print(decimal)
end
-- Function to convert octal to decimal
function octToDec(n)
- local decimal = 0
- local power = 1
- while n > 0 do
- local lastDigit = n % 10
- decimal = decimal + lastDigit * power
- power = power * 8
- n = math.floor(n / 10)
+ local decimal = 0 -- decimal number
+ local power = 1 -- power of 8
+ while n > 0 do -- loop while n is greater than 0
+ local lastDigit = n % 10 -- get the last digit
+ decimal = decimal + lastDigit * power -- add last digit times power to decimal
+ power = power * 8 -- multiply power by 8
+ n = math.floor(n / 10) -- divide n by 10 and round down
end
- print(decimal)
+ print(decimal) -- print decimal number
end
-- Function to convert hexadecimal to decimal
function hexToDec(hexVal)
+ -- Get the length of the hex value
local len = string.len(hexVal)
+ -- Set the base value to 1
local base = 1
+ -- Set the decimal value to 0
local dec_val = 0
+ -- Loop through each character of the hex value
for i = len, 1, -1 do
+ -- Get the character code of the character
local charCode = string.byte(hexVal, i)
+ -- If the character code is 0-9
if charCode >= 48 and charCode <= 57 then
+ -- Get the decimal value of the character and add it to the decimal value
dec_val = dec_val + (charCode - 48) * base
+ -- Multiply the base by 16
base = base * 16
+ -- If the character code is A-F
elseif charCode >= 65 and charCode <= 70 then
+ -- Get the decimal value of the character and add it to the decimal value
dec_val = dec_val + (charCode - 55) * base
+ -- Multiply the base by 16
base = base * 16
end
end
+ -- Print the decimal value
print(dec_val)
end
@@ -126,12 +169,14 @@ function binToOct(n)
local octal = 0
local decimal = 0
local power = 1
+ -- convert binary to decimal
while n > 0 do
local lastDigit = n % 10
decimal = decimal + lastDigit * power
power = power * 2
n = math.floor(n / 10)
end
+ -- convert decimal to octal
power = 1
while decimal > 0 do
local lastDigit = decimal % 8
@@ -144,6 +189,7 @@ end
-- Function to convert binary to hexadecimal
function binToHex(n)
+ -- Convert binary to decimal
local decimal = 0
local power = 1
while n > 0 do
@@ -152,6 +198,7 @@ function binToHex(n)
power = power * 2
n = math.floor(n / 10)
end
+ -- Convert decimal to hexadecimal
local hexaDeciNum = {}
local i = 1
while decimal ~= 0 do
@@ -164,6 +211,7 @@ function binToHex(n)
i = i + 1
decimal = math.floor(decimal / 16)
end
+ -- Print hexadecimal
for j = #hexaDeciNum, 1, -1 do
io.write(hexaDeciNum[j])
end
@@ -172,29 +220,30 @@ end
-- Function to convert octal to binary
function octToBin(n)
- local decimal = 0
- local power = 1
- while n > 0 do
- local lastDigit = n % 10
- decimal = decimal + lastDigit * power
- power = power * 8
- n = math.floor(n / 10)
+ local decimal = 0 -- initialize decimal as 0
+ local power = 1 -- initialize power as 1
+ while n > 0 do -- while n is greater than 0
+ local lastDigit = n % 10 -- take the last digit of n
+ decimal = decimal + lastDigit * power -- add the last digit times the power to decimal
+ power = power * 8 -- multiply power by 8
+ n = math.floor(n / 10) -- divide n by 10 and take the floor
end
- local binaryNum = {}
- local i = 1
- while decimal > 0 do
- binaryNum[i] = decimal % 2
- decimal = math.floor(decimal / 2)
- i = i + 1
+ local binaryNum = {} -- initialize binaryNum as an empty table
+ local i = 1 -- initialize i as 1
+ while decimal > 0 do -- while decimal is greater than 0
+ binaryNum[i] = decimal % 2 -- set the ith element of binaryNum to the last digit of decimal
+ decimal = math.floor(decimal / 2) -- divide decimal by 2 and take the floor
+ i = i + 1 -- increment i
end
- for j = #binaryNum, 1, -1 do
- io.write(binaryNum[j])
+ for j = #binaryNum, 1, -1 do -- for each element in binaryNum in reverse
+ io.write(binaryNum[j]) -- write the jth element of binaryNum
end
- io.write("\n")
+ io.write("\n") -- write a newline
end
-- Function to convert octal to hexadecimal
function octToHex(n)
+ -- Convert octal to decimal
local decimal = 0
local power = 1
while n > 0 do
@@ -203,6 +252,8 @@ function octToHex(n)
power = power * 8
n = math.floor(n / 10)
end
+
+ -- Convert decimal to hexadecimal
local hexaDeciNum = {}
local i = 1
while decimal ~= 0 do
@@ -223,47 +274,72 @@ end
-- Function to convert hexadecimal to binary
function hexToBin(hexVal)
+ -- Get the length of the hex value
local len = string.len(hexVal)
+ -- Create a variable to store the base value
local base = 1
+ -- Create a variable to store the decimal value
local dec_val = 0
+ -- Loop through the hex value starting with the last character
for i = len, 1, -1 do
+ -- Get the ASCII code of the character
local charCode = string.byte(hexVal, i)
+ -- If the ASCII code is between 0 and 9 (ASCII codes 48 to 57)
if charCode >= 48 and charCode <= 57 then
+ -- Add the decimal value of the character to the decimal value
dec_val = dec_val + (charCode - 48) * base
+ -- Multiply the base value by 16
base = base * 16
+ -- If the ASCII code is between A and F (ASCII codes 65 to 70)
elseif charCode >= 65 and charCode <= 70 then
+ -- Add the decimal value of the character to the decimal value
dec_val = dec_val + (charCode - 55) * base
+ -- Multiply the base value by 16
base = base * 16
end
end
+ -- Create a table to store the binary number
local binaryNum = {}
+ -- Create a variable to store the index of the table
local i = 1
+ -- Loop while the decimal value is greater than 0
while dec_val > 0 do
+ -- Add the remainder of the decimal value divided by 2 to the table
binaryNum[i] = dec_val % 2
+ -- Update the decimal value
dec_val = math.floor(dec_val / 2)
+ -- Increment the index of the table
i = i + 1
end
+ -- Loop through the binary number table starting with the last element
for j = #binaryNum, 1, -1 do
+ -- Print the binary number
io.write(binaryNum[j])
end
- io.write("\n")
-end
+ -- Print a newline
-- Function to convert hexadecimal to octal
function hexToOct(hexVal)
+ -- Get the length of the string
local len = string.len(hexVal)
+ -- Initialize the base to 1 and the decimal value to 0
local base = 1
local dec_val = 0
+ -- Loop through each character in the string
for i = len, 1, -1 do
+ -- Get the character code of the current character
local charCode = string.byte(hexVal, i)
+ -- If the character is a number (0-9), convert to decimal
if charCode >= 48 and charCode <= 57 then
dec_val = dec_val + (charCode - 48) * base
base = base * 16
+ -- If the character is a letter (A-F), convert to decimal
elseif charCode >= 65 and charCode <= 70 then
dec_val = dec_val + (charCode - 55) * base
base = base * 16
end
end
+ -- Convert the decimal value to octal
local octalNum = {}
local i = 1
while dec_val ~= 0 do
@@ -271,6 +347,7 @@ function hexToOct(hexVal)
dec_val = math.floor(dec_val / 8)
i = i + 1
end
+ -- Print the octal number
for j = #octalNum, 1, -1 do
io.write(octalNum[j])
end
diff --git a/Number System/readme.md b/Number System/readme.md
index b50943ae..daf8e5e2 100644
--- a/Number System/readme.md
+++ b/Number System/readme.md
@@ -251,3 +251,14 @@ Thus $77_{10} = 302_{5}$.
## [Mixed-Base Number Systems](#mixed-base-number-systems)
Mixed-base number systems are number systems that use more than one base. Such numerical representation applies when a quantity is expressed using a sequence of units that are each a multiple of the next smaller one, but not by the same factor. The easiet example of a mixed-base number system is from our timekeeping methods. 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day, 7 days in a week, 4 weeks in a month, 12 months in a year, 10 years in a decade, 10 decades in a century, 10 centuries in a millennium. All of these are different bases and they are all combined to make our timekeeping system.
+
+
+## Code for Conversion
+
+In C [Conversion_In_C.c](./Conversion_In_C.c)
+In C++ [Conversion_In_C++.cpp](./Conversion_In_c++.cpp)
+In Java [Conversion_In_Java.java](./Conversion_In_Java.java)
+In JS [Conversion_In_JS.js](./Conversion_In_JS.js)
+In TS [Conversion_In_TS.ts](./Conversion_In_TS.ts)
+In Lua [Conversion_In_Lua.lua](./Conversion_In_Lua.lua)
+
\ No newline at end of file
From 2b59571d3804037a2783695d831a2e052415e247 Mon Sep 17 00:00:00 2001
From: sifat
Date: Fri, 6 Oct 2023 02:04:31 -0700
Subject: [PATCH 4/6] Update readme.md
---
Number System/readme.md | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/Number System/readme.md b/Number System/readme.md
index daf8e5e2..5f8d0edc 100644
--- a/Number System/readme.md
+++ b/Number System/readme.md
@@ -193,8 +193,8 @@ Thus, the equivalent decimal number for the given binary number (1101) is **(13)
First, you need to convert a binary into other base system (e.g., into decimal, or into octal). Then you need to convert it hexadecimal number.Since number numbers are type of positional number system. That means weight of the positions from right to left are as 160^, 16^1, 16^2, 16^3 and so on. for the integer part and weight of the positions from left to right are as 16^-1, 16^-2, 16^-3 and so on. for the fractional part.
### [Example of Binary to Hexadecimal Conversion](#example-binary/hexadecimal)
-Converting binary number 1101010 into hexadecimal number.
-First convert this into decimal number:
+Converting binary number 1101010 into hexadecimal numbers.
+First, convert this into a decimal number:
= (1101010)
@@ -255,10 +255,9 @@ Mixed-base number systems are number systems that use more than one base. Such n
## Code for Conversion
-In C [Conversion_In_C.c](./Conversion_In_C.c)
-In C++ [Conversion_In_C++.cpp](./Conversion_In_c++.cpp)
-In Java [Conversion_In_Java.java](./Conversion_In_Java.java)
-In JS [Conversion_In_JS.js](./Conversion_In_JS.js)
-In TS [Conversion_In_TS.ts](./Conversion_In_TS.ts)
-In Lua [Conversion_In_Lua.lua](./Conversion_In_Lua.lua)
-
\ No newline at end of file
+- [Conversion In C](./Conversion_In_C.c)
+- [Conversion In C++](./Conversion_In_c++.cpp)
+- [Conversion In Java](./Conversion_In_Java.java)
+- [Conversion In JS](./Conversion_In_JS.js)
+- [Conversion In TS](./Conversion_In_TS.ts)
+- [Conversion In Lua](./Conversion_In_Lua.lua)
From 865cd48ad3edf85ff00d716fa8ef0889c1189cc5 Mon Sep 17 00:00:00 2001
From: Sarthak950
Date: Sun, 8 Oct 2023 07:52:32 +0530
Subject: [PATCH 5/6] updated the 404 and programing languages README
---
404.md | 28 +++++--
Programming_Languages/readme.md | 127 ++++++++++++++++++++++++++++++--
2 files changed, 143 insertions(+), 12 deletions(-)
diff --git a/404.md b/404.md
index 8ba2b40f..92a4745b 100644
--- a/404.md
+++ b/404.md
@@ -1,10 +1,24 @@
-# 404 Not Found
-This section is not added. You can contribute by adding content to this section.
+## Error Codes
+While browsing the internet, you may have encountered some error codes.
+Like:
+* 404 Not Found
+* 403 Forbidden
+* 500 Internal Server Error
+* 502 Bad Gateway
+* 503 Service Unavailable
+* 504 Gateway Timeout
+* 408 Request Timeout
-## Contributing
-You can contribute by adding content to this section. You can also contribute by adding a new section to the website. To do so, you can follow the steps below:
+These errors might be confusing for you. But they make the life of the developer a lot easier
+as they contain a lot of useful information about the problem.
-1. Fork this repository.
-2. Add the content to the section you want to contribute to.
-3. Create a pull request.
+In genral the error codes means:
+* 1xx: informational response - it means the request was received and the process is continuing
+* 2xx: success - the request was successfully received, understood, and accepted
+* 3xx: redirection - further action needs to be taken in order to complete the request
+* 4xx: client error - the request contains bad syntax or cannot be fulfilled
+* 5xx: server error - the server failed to fulfil an apparently valid request
+
+### 404 Not Found
+This error means that the requested resource could not be found. This is the most common error you will encounter while browsing the internet. This error is returned when the server is unable to find the requested resource. This error can be caused by a number of things, including a broken link, a mistyped URL, or a deleted file.
\ No newline at end of file
diff --git a/Programming_Languages/readme.md b/Programming_Languages/readme.md
index 17397ade..4984d01a 100644
--- a/Programming_Languages/readme.md
+++ b/Programming_Languages/readme.md
@@ -1,5 +1,7 @@
# Programming Languages
+>A programming language is a formal language comprising a set of instructions that produce various kinds of output. Programming languages are used in computer programming to implement algorithms.
+
Programming languages are mainly of two types as mentioned before:
1. High-level language uses a syntax similar to the English language. The source code is converted into machine-understandable machine code using a compiler or an interpreter. Java and Python are some examples of high-level programming languages. These are usually slower than Low-level, but it comes with being easier.
2. Low-level programming languages work more closely with the hardware and have more control over it. They directly interact with the hardware. Two common examples of low-level languages are machine language and assembly language. These are usually faster than High-level, but it comes at the cost of very great difficulty.
@@ -13,19 +15,41 @@ Some Languages Include:
- [C#](No-Website)
- [Go](https://www.go.dev/)
- [Swift](https://developer.apple.com/swift/)
+- [Assembly](https://en.wikipedia.org/wiki/X86_assembly_language)
### Scripting Languages
A scripting language is a computer language that doesn't have to be compiled. One advantage of Scripting languages is that the code can be quickly edited and can be re-executed. They can be used on the server side of web applications and the client side of web applications.
Some Scripting Languages include:
- [JavaScript](https://www.javascript.com/)
-- [PHP](https://www.php.net/)- [Java](https://www.java.com/)
-- [JavaScript](https://www.javascript.com/)
+- [Python](https://www.python.org)
+- [Node.js](https://nodejs.org/en/)
+- [Bash](https://www.gnu.org/software/bash/)
+- [Ruby](https://www.ruby-lang.org/en/)
+- [Perl](https://www.perl.org/)
+
+
+### Compiled Languages
+A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place). The term is somewhat vague. In principle, any language can be implemented with a compiler or with an interpreter.
+
+Some Compiled Languages include:
+- [C](https://en.wikipedia.org/wiki/C_(programming_language))
- [C++](https://www.cplusplus.com/)
-- [C](No-Website)
-- [C#](No-Website)
+- [C#](https://learn.microsoft.com/en-us/dotnet/csharp/)
+- [Rust](https://www.rust-lang.org/)
- [Go](https://www.go.dev/)
-- [Swift](https://developer.apple.com/swift/)
+
+### Interpreted Languages
+An interpreted language is a type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions. The interpreter executes the program directly, translating each statement into a sequence of one or more subroutines, and then into another language (often machine code).
+
+Some Interpreted Languages include:
+- [Python](https://www.python.org)
+- [JavaScript](https://www.javascript.com/)
+- [Ruby](https://www.ruby-lang.org/en/)
+- [PHP](https://www.php.net/)
+- [Perl](https://www.perl.org/)
+- [Lua](https://www.lua.org/)
+- [R](https://www.r-project.org/)
### Programming Language Paradigms
@@ -54,6 +78,21 @@ We are telling the program to iterate through each element in the array, compare
**Procedural programming**: Prodecural programming is a derivation of imperative programming, adding to it the features of functions (also known as "prodecures or "subroutines"). In procedural programming, the user is encouragd to subdivide the program execution into proecudures, as a way of improving modularity and organization.
+```javascript
+const nums = [1,4,3,6,7,8,9,2]
+
+function filterNums() {
+ const result = []
+
+ for (let i=0;i < nums.length;i++) {
+ if(nums[i] > 5) result.push(nums[i])
+ }
+ return result
+}
+```
+
+In this example, we've created a function that takes care of the array filtering. This way, we can reuse the function in other parts of our code, and we can also make sure that the function doesn't modify anything outside its scope.
+
**Functional programming**: Functional programming takes the concept of functions a little bit further. Functions are treated as **first-class citizens**, meaning they can be assigned to variables, passed as arguments, and returned from other functions. Functional programming defends the idea that code modularity and the absence of side effects make it easier to identify and separate responsibilities within the codebase. This, therefore, improves code maintainability. Going back to the array filtering example, we can see that with the imperative paradigm, we might use an external variable to store the function's result, which can be considered a side effect.
```javascript
@@ -102,3 +141,81 @@ as objects, Each entity will group a given set of information (properties) and a
OOP makes heavy use of classes, which are a way of creating new objects starting out from a blueprint or boilerplate that the programmer sets. Objects that are created from a class are called instances.
+**Markup Languages**
+Markup languages are a way of annotating a document in a way that is syntactically distinguishable from the text. The idea is to add extra information to the text, such as formatting information, metadata, or instructions for the application that will read the document. Markup languages are often used to format documentation, such as HTML, which is used to format web pages.
+
+Some Markup Languages include:
+- [HTML](https://developer.mozilla.org/en-US/docs/Web/HTML)
+- [XML](https://www.w3.org/XML/)
+- [LaTeX](https://www.latex-project.org/)
+- [Markdown](https://www.markdownguide.org/)
+- [YAML](https://yaml.org/)
+- [JSON](https://www.json.org/json-en.html)
+
+**Styling languages**
+Styling languages are used to add style to markup languages. They are used to add colors, fonts, and other visual styles to web pages and documents.
+
+Some Styling Languages include:
+- [CSS](https://developer.mozilla.org/en-US/docs/Web/CSS)
+- [SASS](https://sass-lang.com/)
+- [SCSS](https://sass-lang.com/)
+- [LESS](http://lesscss.org/)
+- [Stylus](https://stylus-lang.com/)
+
+
+
+### Genration of Programming Languages
+Programming languages are often classified by generation. The generation of a programming language is a term used to refer to the stage of development of a programming language. There are five generations of programming languages:
+
+1. **First generation**: Machine language is the only language that the computer understands. It is a language made up of entirely 0s and 1s. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language. It is the only language that the computer understands, and it is tedious to write and understand. It is also known as the machine language.
+```Hello World is Machine Language
+ b8 21 0a 00 00 #moving "!\n" into eax
+ a3 0c 10 00 06 #moving eax into first memory location
+ b8 6f 72 6c 64 #moving "orld" into eax
+ a3 08 10 00 06 #moving eax into next memory location
+ b8 6f 2c 20 57 #moving "o, W" into eax
+ a3 04 10 00 06 #moving eax into next memory location
+ b8 48 65 6c 6c #moving "Hell" into eax
+ a3 00 10 00 06 #moving eax into next memory location
+ b9 00 10 00 06 #moving pointer to start of memory location into ecx
+ ba 10 00 00 00 #moving string size into edx
+ bb 01 00 00 00 #moving "stdout" number to ebx
+ b8 04 00 00 00 #moving "print out" syscall number to eax
+ cd 80 #calling the linux kernel to execute our print to stdout
+ b8 01 00 00 00 #moving "sys_exit" call number to eax
+ cd 80 #executing it via linux sys_call
+```
+2. **Second generation**: Assembly language is the second generation of programming. It is a language that is easier to understand than machine language. It consists of a set of instructions that are converted into machine language by an assembler.
+```Hello World is Assambly Language
+ .data
+ msg db 'Hello World!', 0xa
+ len equ $ - msg
+ .text
+ global _start
+ _start:
+ mov eax, 4
+ mov ebx, 1
+ mov ecx, msg
+ mov edx, len
+ int 0x80
+ mov eax, 1
+ mov ebx, 0
+ int 0x80
+```
+3. **Third generation**: High-level languages are the third generation of programming languages. They are easier to understand, easier to write, and easier to maintain. They are also portable, which means that they can be run on different types of computers. Examples of high-level languages include C, C++, Java, and Python.
+```Hello World is High Level Language C
+ #include
+ int main() {
+ printf("Hello, World!");
+ return 0;
+ }
+```
+4. **Fourth generation**: Fourth-generation languages are programming languages that are designed to be used by non-programmers. They are often used to create database queries and reports. Examples of fourth-generation languages include SQL, SAS, and SPSS.
+```Hello World is Fourth Generation Language SQL
+ SELECT 'Hello, World!';
+```
+5. **Fifth generation**: Fifth-generation languages are programming languages that are designed to be used by non-programmers. These are the programming languages that have visual tools to develop a program. Examples of fifth-generation languages include Mercury, OPS5, and Prolog.
+```Hello world in Prolog
+ :- initialization(main).
+ main :- write('Hello, World!'), nl, halt.
+```
From 81835739161e497d1f1ec569786d0898fcb61ecf Mon Sep 17 00:00:00 2001
From: sifat
Date: Sun, 8 Oct 2023 05:36:21 -0700
Subject: [PATCH 6/6] Update 404.md
---
404.md | 28 +++++++---------------------
1 file changed, 7 insertions(+), 21 deletions(-)
diff --git a/404.md b/404.md
index 92a4745b..8ba2b40f 100644
--- a/404.md
+++ b/404.md
@@ -1,24 +1,10 @@
-## Error Codes
-While browsing the internet, you may have encountered some error codes.
-Like:
-* 404 Not Found
-* 403 Forbidden
-* 500 Internal Server Error
-* 502 Bad Gateway
-* 503 Service Unavailable
-* 504 Gateway Timeout
-* 408 Request Timeout
+# 404 Not Found
+This section is not added. You can contribute by adding content to this section.
-These errors might be confusing for you. But they make the life of the developer a lot easier
-as they contain a lot of useful information about the problem.
+## Contributing
+You can contribute by adding content to this section. You can also contribute by adding a new section to the website. To do so, you can follow the steps below:
-In genral the error codes means:
-* 1xx: informational response - it means the request was received and the process is continuing
-* 2xx: success - the request was successfully received, understood, and accepted
-* 3xx: redirection - further action needs to be taken in order to complete the request
-* 4xx: client error - the request contains bad syntax or cannot be fulfilled
-* 5xx: server error - the server failed to fulfil an apparently valid request
+1. Fork this repository.
+2. Add the content to the section you want to contribute to.
+3. Create a pull request.
-
-### 404 Not Found
-This error means that the requested resource could not be found. This is the most common error you will encounter while browsing the internet. This error is returned when the server is unable to find the requested resource. This error can be caused by a number of things, including a broken link, a mistyped URL, or a deleted file.
\ No newline at end of file