diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index 19bf37dd..448cdc18 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -5,3 +5,35 @@ Insert first number: 8 The binary number is: 1000 */ +#include + +void decToBinary(int n) { + if (n == 0) { + printf("0"); + return; + } + + int binary[32]; + int i = 0; + + while (n > 0) { + binary[i] = n % 2; + n = n / 2; + i++; + } + + for (int j = i - 1; j >= 0; j--) + printf("%d", binary[j]); +} + +int main() { + int num; + printf("Insert first number: \n"); + scanf("%d", &num); + + printf("The binary number is: \n"); + decToBinary(num); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 013cc786..0245930e 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -10,3 +10,25 @@ Multiplication: 8 Division: 2 */ +#include + +int main() { + float num1, num2; + + printf("Insert first number: "); + scanf("%f", &num1); + + printf("Insert second number: "); + scanf("%f", &num2); + + printf("\nSUM: %.2f", num1 + num2); + printf("\nDifference: %.2f", num1 - num2); + printf("\nMultiplication: %.2f", num1 * num2); + + if (num2 != 0) + printf("\nDivision: %.2f\n", num1 / num2); + else + printf("\nDivision: impossible (division by zero)\n"); + + return 0; +}