From 3409df4e156e9273f6ee9c889106ea23b45f75cb Mon Sep 17 00:00:00 2001 From: SinusFox Date: Wed, 10 May 2023 09:35:11 +0200 Subject: [PATCH 1/2] added exercise 3 --- 3_Simple_Calculation/Simple_Calculation.c | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 3_Simple_Calculation/Simple_Calculation.c diff --git a/3_Simple_Calculation/Simple_Calculation.c b/3_Simple_Calculation/Simple_Calculation.c new file mode 100644 index 0000000..52191f3 --- /dev/null +++ b/3_Simple_Calculation/Simple_Calculation.c @@ -0,0 +1,32 @@ +#include + +int main() { + // creating variables + int first = 0, second = 0, addition = 0, subtraction = 0, multiplication = 0, modulo = 0; + double division = 0; + + // receiving numbers from user + printf("Please type in the first integer: "); + scanf("%i", &first); + printf("Please type in the second integer: "); + scanf("%i", &second); + + // checking numbers + printf("Your input: %i and %i\nCalculating...\n\n", first, second); + + // calculating + addition = first + second; + subtraction = first - second; + multiplication = first * second; + if (second != 0) { + division = first / second; + } else { + printf("\n\nError: Dividing by 0 is not allowed. Output will be 0.\n\n"); + } + modulo = first % second; + + // output of calculation + printf("The results are:\n+ = %i\n- = %i\n* = %i\n/ = %f\nmod = %i", addition, subtraction, multiplication, division, modulo); + + return 0; +} -- 2.52.0 From ea2aef7ed14c56d005b139ef49ad08fdc96751a0 Mon Sep 17 00:00:00 2001 From: SinusFox Date: Wed, 10 May 2023 09:42:58 +0200 Subject: [PATCH 2/2] fix: modulo by zero --- 3_Simple_Calculation/Simple_Calculation.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/3_Simple_Calculation/Simple_Calculation.c b/3_Simple_Calculation/Simple_Calculation.c index 52191f3..9adbc35 100644 --- a/3_Simple_Calculation/Simple_Calculation.c +++ b/3_Simple_Calculation/Simple_Calculation.c @@ -20,10 +20,10 @@ int main() { multiplication = first * second; if (second != 0) { division = first / second; + modulo = first % second; } else { - printf("\n\nError: Dividing by 0 is not allowed. Output will be 0.\n\n"); + printf("\nError: Dividing by 0 is not allowed. Output will be 0 for devision and modulo.\n\n"); } - modulo = first % second; // output of calculation printf("The results are:\n+ = %i\n- = %i\n* = %i\n/ = %f\nmod = %i", addition, subtraction, multiplication, division, modulo); -- 2.52.0