From 2193d60bb53932810b6d59eb2a17e147da22f89b Mon Sep 17 00:00:00 2001 From: Yogiraj Shinde <84953719+yogirajbshinde21@users.noreply.github.com> Date: Sun, 7 May 2023 15:51:06 +0530 Subject: [PATCH] Update calculator.c to display full calculation. In this contribution, I added more descriptive output to the calculation results. Now, the program will display the full calculation entered by the user along with the result. This makes the program more informative and user-friendly. Additionally, I added an extra check to the '%' operator case to prevent division by zero errors. --- c_programming/calculator.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/c_programming/calculator.c b/c_programming/calculator.c index 545061059..0da88e5dd 100644 --- a/c_programming/calculator.c +++ b/c_programming/calculator.c @@ -8,32 +8,33 @@ double num1, num2; char op; printf("Enter the calculation:\n"); -scanf("%lf%c%lf", &num1,&op,&num2); +scanf("%lf %c %lf", &num1,&op,&num2); switch(op){ case '+': - printf("=%lf\n", num1+num2); + printf("%lf + %lf = %lf\n", num1, num2, num1+num2); break; case '-': - printf("=%lf\n", num1-num2); + printf("%lf - %lf = %lf\n", num1, num2, num1-num2); break; case '*': - printf("=%lf\n", num1*num2); + printf("%lf * %lf = %lf\n", num1, num2, num1*num2); break; case '/': if(num2==0){ printf("Division by zero error!\n"); }else{ - printf("=%lf\n", num1/num2); + printf("%lf / %lf = %lf\n", num1, num2, num1/num2); } break; case '%': - printf("=%l\n", (long)num1%(long)num2); + if((long)num2==0){ + printf("Division by zero error!\n"); + }else{ + printf("%lf %% %lf = %ld\n", num1, num2, (long)num1%(long)num2); + } break; default: printf("Invalid Operator\n"); } } - - -