Przeglądaj źródła

added binary tasks

Noah Vogt 3 lat temu
rodzic
commit
0c987fc60f

+ 22 - 0
inputOutputFlanagan/binary2dec.java

@@ -0,0 +1,22 @@
+import flanagan.io.KeyboardInput;
+import java.math.BigInteger;
+import java.lang.Math;
+
+public class binary2dec{
+
+    public static void main(String arg[]) {
+        KeyboardInput kb = new KeyboardInput();
+        String binaryNumber = kb.readLine("binary number =");
+
+        int currentNumber, sum = 0;
+
+        for (int i = 0; i < binaryNumber.length(); i++) {
+            currentNumber = Integer.parseInt(binaryNumber.substring(i,i + 1));
+            if (currentNumber == 1) {
+                sum += Math.pow(2, binaryNumber.length() - i - 1);
+            }
+        }
+
+        System.out.println(sum);
+    }
+}

+ 31 - 0
inputOutputFlanagan/dec2binary.java

@@ -0,0 +1,31 @@
+import flanagan.io.KeyboardInput;
+import java.math.BigInteger;
+import java.lang.Math;
+
+public class dec {
+    public static void main(String arg[]) {
+        KeyboardInput kb = new KeyboardInput();
+        int decimalNumber = kb.readInt("decimal number =");
+
+        int currentNumber, sum = 0, cur = 0; i = 0;
+
+        while (true) {
+            if (decimalNumber - Math.pow(2,i) < 0) {
+                break;
+            }
+            i++;
+        }
+
+        String binary = "";
+        for (int j = i - 1; j >= 0; j--) {
+            if (decimalNumber - Math.pow(2,j) < 0) {
+                binary += "0";
+            } else {
+                binary += "1";
+                decimalNumber -= Math.pow(2,j);
+            }
+        }
+        
+        System.out.println(binary);
+    }
+}

+ 16 - 0
inputOutputFlanagan/modulo.java

@@ -0,0 +1,16 @@
+import flanagan.io.KeyboardInput;
+import java.math.BigInteger;
+
+public class modulo {
+    public static void main(String arg[]) {
+        KeyboardInput kb = new KeyboardInput();
+        BigInteger a = kb.readBigInteger("a =", 1);
+        int b = kb.readInt("b =", 1);
+
+        BigInteger r = a.mod(BigInteger.valueOf(b));
+        BigInteger k = a.subtract(r).divide(BigInteger.valueOf(b));
+
+        System.out.println("a = k * b + r");
+        System.out.printf("k = %d, r = %d\n", k, r);
+    }
+}

+ 20 - 0
inputOutputFlanagan/quersumme.java

@@ -0,0 +1,20 @@
+import flanagan.io.KeyboardInput;
+import java.math.BigInteger;
+
+public class quersumme {
+    public static void main(String arg[]) {
+        KeyboardInput kb = new KeyboardInput();
+        String decimalNumber = kb.readLine("decimal number =");
+        
+        int sum = 0;
+        String currentDigit;
+        
+        for (int i = 0; i < decimalNumber.length(); i++) {
+            currentDigit = decimalNumber.substring(i, i+1);
+            System.out.printf(currentDigit + " ");
+            sum += Integer.parseInt(currentDigit);
+        }
+        
+        System.out.printf("\nquersumme = %d\n", sum);
+    }
+}