Skip to content

Commit

Permalink
style: indented code
Browse files Browse the repository at this point in the history
  • Loading branch information
EmyCodes committed Jul 30, 2023
1 parent d5bc7ce commit 2a4f0d8
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions emycodes/emycodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,60 @@
* The program displays the result with two decimal places.
* Proper input validation and error handling
* for invalid operators and division by zero are implemented.
*/
*/

int main(void)
{
char operator;
double num1, num2, result;
char operator;
double num1, num2, result;

/* Introduction and Input Gathering */
printf("Simple Calculator\n");
printf("Enter first number: ");
scanf("%lf", &num1);
/* Introduction and Input Gathering */
printf("Simple Calculator\n");
printf("Enter first number: ");
scanf("%lf", &num1);

/* Operator Input Validation Loop */
while (1)
{
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
/* Operator Input Validation Loop */
while (1)
{
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);

/* Check for valid operator input */
if (operator == '+' || operator == '-' || operator == '*' || operator == '/')
break;
else
printf("Error: Invalid operator. Please try again.\n\n");
}
/* Check for valid operator input */
if (operator == '+' || operator == '-' || operator == '*' || operator == '/')
break;
else
printf("Error: Invalid operator. Please try again.\n\n");
}


printf("Enter second number: ");
scanf("%lf", &num2);
printf("Enter second number: ");
scanf("%lf", &num2);

/* Perform the selected operation */
switch (operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
/* Division by zero check*/
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero is not allowed.\n");
return 1; /* Exit the program with an error code */
}
break;
}
/* Perform the selected operation */
switch (operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
/* Division by zero check*/
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero is not allowed.\n");
return 1; /* Exit the program with an error code */
}
break;
}

/* Output the result */
printf("Result: %lf\n", result);
/* Output the result */
printf("Result: %lf\n", result);

return (0);
return (0);
}

0 comments on commit 2a4f0d8

Please sign in to comment.