Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 2 snippets #193

Closed
wants to merge 11 commits into from
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/math/Binary2DecimalSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package math;
import java.util.Scanner;

/** Binary to Decimal Conversion
* Snippet
*/
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);
}
}
37 changes: 37 additions & 0 deletions src/main/java/math/Decimal2BinarySnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package math;
import java.util.Scanner;
/** Binary to Decimal Conversion
* Snippet
*/
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);
}
}
22 changes: 22 additions & 0 deletions src/test/java/math/Binary2DecimalSnippetTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
22 changes: 22 additions & 0 deletions src/test/java/math/Decimal2BinarySnippetTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading