From 71025f12759d3f35a9f62429ab324a4d4c5fd840 Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:49:33 +0530 Subject: [PATCH 01/10] Added 2 snippets BInary to Decimal and Decimal to Binary conversion snippets --- src/main/java/math/Binary2Decimal.java | 44 ++++++++++++++++++++++++ src/main/java/math/Decimal2Binary.java | 46 ++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/math/Binary2Decimal.java create mode 100644 src/main/java/math/Decimal2Binary.java diff --git a/src/main/java/math/Binary2Decimal.java b/src/main/java/math/Binary2Decimal.java new file mode 100644 index 00000000..83ba2a6c --- /dev/null +++ b/src/main/java/math/Binary2Decimal.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +/** Binary to Decimal Conversion + * Snippet + */ +public class Binary2Decimal { + /** + * function to reverse a string + * @param str string + * @return rev reversed string + */ + public static String reverse(String str) { + String rev=""; + for (int i = str.length()-1; i >= 0; i--) { + char ch = str.charAt(i); + rev += ch; + } + return rev; + } + + /** + * converts binary to decimal + * @param n number + */ + public static void binaryToDecimal(int n) { + String s = reverse(Integer.toString(n)); + int decimal=0; + + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + if(ch=='1') { + decimal += Math.pow(2, i); + } + } + System.out.println(decimal); + } + + public static void main(String[] args) { + System.out.println("Enter a Number: "); + Scanner sc = new Scanner(System.in); + int number = sc.nextInt(); + binaryToDecimal(number); + } +} diff --git a/src/main/java/math/Decimal2Binary.java b/src/main/java/math/Decimal2Binary.java new file mode 100644 index 00000000..3bd3e7ed --- /dev/null +++ b/src/main/java/math/Decimal2Binary.java @@ -0,0 +1,46 @@ +import java.util.Scanner; +/** Binary to Decimal Conversion + * Snippet + */ +public class Decimal2Binary { + /** + * reverse a string + * @param str string + * @return rev reversed string + */ + public static String reverse(String str) { + String rev=""; + for (int i = str.length()-1; i >= 0; i--) { + char ch = str.charAt(i); + rev += ch; + } + return rev; + } + + /** + * converts decimal to binary + * @param n number + */ + public static void decimalToBinary(int n) { + int d; + String dec=""; + while(n!=0) { + d = n%2; + dec += Integer.toString(d); + n /= 2; + } + + String binary = reverse(dec); + System.out.println(binary); + } + + + public static void main(String[] args) { + // user input + System.out.println("Enter a Number: "); + Scanner sc = new Scanner(System.in); + int number = sc.nextInt(); + + decimalToBinary(number); + } +} From c4a2cbffa75e656f89469e2bb9f664071ab5f2db Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:09:19 +0530 Subject: [PATCH 02/10] Update Decimal2Binary.java --- src/main/java/math/Decimal2Binary.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/math/Decimal2Binary.java b/src/main/java/math/Decimal2Binary.java index 3bd3e7ed..3ea735eb 100644 --- a/src/main/java/math/Decimal2Binary.java +++ b/src/main/java/math/Decimal2Binary.java @@ -33,14 +33,4 @@ public static void decimalToBinary(int n) { String binary = reverse(dec); System.out.println(binary); } - - - public static void main(String[] args) { - // user input - System.out.println("Enter a Number: "); - Scanner sc = new Scanner(System.in); - int number = sc.nextInt(); - - decimalToBinary(number); - } } From 7b4c8a24c8a1d8136ee4e3ef8efcde90d7022e0a Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:09:38 +0530 Subject: [PATCH 03/10] Update Binary2Decimal.java --- src/main/java/math/Binary2Decimal.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/math/Binary2Decimal.java b/src/main/java/math/Binary2Decimal.java index 83ba2a6c..c99efa45 100644 --- a/src/main/java/math/Binary2Decimal.java +++ b/src/main/java/math/Binary2Decimal.java @@ -34,11 +34,4 @@ public static void binaryToDecimal(int n) { } System.out.println(decimal); } - - public static void main(String[] args) { - System.out.println("Enter a Number: "); - Scanner sc = new Scanner(System.in); - int number = sc.nextInt(); - binaryToDecimal(number); - } } From 1055886272861f401cd019d87b8ccc9925e92c39 Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:17:22 +0530 Subject: [PATCH 04/10] Create Binary2DecimalSnippetTest.java Tests for the Binary2DecimalSnippet.java code --- .../java/math/Binary2DecimalSnippetTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/test/java/math/Binary2DecimalSnippetTest.java diff --git a/src/test/java/math/Binary2DecimalSnippetTest.java b/src/test/java/math/Binary2DecimalSnippetTest.java new file mode 100644 index 00000000..9bcae292 --- /dev/null +++ b/src/test/java/math/Binary2DecimalSnippetTest.java @@ -0,0 +1,22 @@ +package math; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/* + * Tests for 30 Seconds of Java code library + * + */ +class Binary2DecimalSnippetTest { + /** + * Tests for {@link Binary2DecimalSnippet#binaryToDecimal(int)}. + */ + @Test + void testLcm() { + assertEquals(9, Binary2DecimalSnippet.binaryToDecimal(1001)); + assertEquals(10, Binary2DecimalSnippet.binaryToDecimal(1010)); + assertEquals(72, Binary2DecimalSnippet.binaryToDecimal(1001000)); + assertEquals(7, Binary2DecimalSnippet.binaryToDecimal(111)); + } +} From 3098001db08d27e063b714a001169e99f9d70bce Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:20:23 +0530 Subject: [PATCH 05/10] Create Decimal2BinarySnippetTest.java Tests for the Decimal2Binary.java code --- .../java/math/Decimal2BinarySnippetTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/test/java/math/Decimal2BinarySnippetTest.java diff --git a/src/test/java/math/Decimal2BinarySnippetTest.java b/src/test/java/math/Decimal2BinarySnippetTest.java new file mode 100644 index 00000000..c813239f --- /dev/null +++ b/src/test/java/math/Decimal2BinarySnippetTest.java @@ -0,0 +1,22 @@ +package math; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/* + * Tests for 30 Seconds of Java code library + * + */ +class Binary2DecimalSnippetTest { + /** + * Tests for {@link Decimal2BinarySnippet#decimalToBinary(int)}. + */ + @Test + void testLcm() { + assertEquals(1001, Decimal2BinarySnippet.decimalToBinary(9)); + assertEquals(1010, Decimal2BinarySnippet.decimalToBinary(10)); + assertEquals(1001000, Decimal2BinarySnippet.decimalToBinary(72)); + assertEquals(111, Decimal2BinarySnippet.decimalToBinary(7)); + } +} From 60b5b0ea310fb6d3f852573eae9474b1ea8d4d3b Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:21:07 +0530 Subject: [PATCH 06/10] Renamed the file --- .../java/math/{Binary2Decimal.java => Binary2DecimalSnippet.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/math/{Binary2Decimal.java => Binary2DecimalSnippet.java} (100%) diff --git a/src/main/java/math/Binary2Decimal.java b/src/main/java/math/Binary2DecimalSnippet.java similarity index 100% rename from src/main/java/math/Binary2Decimal.java rename to src/main/java/math/Binary2DecimalSnippet.java From c8f5dcc4e54f605ab1a3571b4dc6504eef9d5f5c Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:21:24 +0530 Subject: [PATCH 07/10] Renamed the file --- .../java/math/{Decimal2Binary.java => Decimal2BinarySnippet.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/math/{Decimal2Binary.java => Decimal2BinarySnippet.java} (100%) diff --git a/src/main/java/math/Decimal2Binary.java b/src/main/java/math/Decimal2BinarySnippet.java similarity index 100% rename from src/main/java/math/Decimal2Binary.java rename to src/main/java/math/Decimal2BinarySnippet.java From 83f289dade0ce30c026d7b598674aca07dc94eee Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:27:30 +0530 Subject: [PATCH 08/10] Update README.md Added 2 snippets - Binary to Decimal and Decimal to Binary converters --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 3f18fa06..2660d7cc 100644 --- a/README.md +++ b/README.md @@ -499,6 +499,48 @@ Inspired by [30 seconds of code](https://github.com/Chalarangelo/30-seconds-of-c ## Math +### Binary To Decimal + +```java + /** + * converts binary to decimal + * @param n number + */ + public static void binaryToDecimal(int n) { + String s = reverse(Integer.toString(n)); + int decimal=0; + + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + if(ch=='1') { + decimal += Math.pow(2, i); + } + } + System.out.println(decimal); + } +``` + +### Decimal To Binary + +```java + /** + * converts decimal to binary + * @param n number + */ + public static void decimalToBinary(int n) { + int d; + String dec=""; + while(n!=0) { + d = n%2; + dec += Integer.toString(d); + n /= 2; + } + + String binary = reverse(dec); + System.out.println(binary); + } +``` + ### Fibonacci ```java From 3b0fcda47b3be01e892de97234edd41a51d5ad25 Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:33:56 +0530 Subject: [PATCH 09/10] Update Binary2DecimalSnippet.java --- src/main/java/math/Binary2DecimalSnippet.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/math/Binary2DecimalSnippet.java b/src/main/java/math/Binary2DecimalSnippet.java index c99efa45..60e2ae7f 100644 --- a/src/main/java/math/Binary2DecimalSnippet.java +++ b/src/main/java/math/Binary2DecimalSnippet.java @@ -1,9 +1,10 @@ +package math; import java.util.Scanner; /** Binary to Decimal Conversion * Snippet */ -public class Binary2Decimal { +class Binary2Decimal { /** * function to reverse a string * @param str string From 094246afad4f6ce450cc5aa90faa8b6da561ad15 Mon Sep 17 00:00:00 2001 From: Srijit Chakraborty <121560359+srijitt@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:34:16 +0530 Subject: [PATCH 10/10] Update Decimal2BinarySnippet.java --- src/main/java/math/Decimal2BinarySnippet.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/math/Decimal2BinarySnippet.java b/src/main/java/math/Decimal2BinarySnippet.java index 3ea735eb..432ea50b 100644 --- a/src/main/java/math/Decimal2BinarySnippet.java +++ b/src/main/java/math/Decimal2BinarySnippet.java @@ -1,8 +1,9 @@ +package math; import java.util.Scanner; /** Binary to Decimal Conversion * Snippet */ -public class Decimal2Binary { +class Decimal2Binary { /** * reverse a string * @param str string