Master Math with Ease: Your Ultimate Guide to the Scientific Calculator Program in C

Introduction:

In this blog, we will discuss a scientific calculator program in the C programming language. The program allows the user to perform various mathematical operations such as addition, subtraction, multiplication, division, exponentiation, square root, and logarithm. The program starts with a welcome message and a list of available operations. The user is then prompted to choose an operation by entering a number from 1 to 8. The program uses a switch statement to perform the selected operation. The user is then prompted to choose another operation or exit the program by entering 8.

Let's look at the code:

The Code

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main() {

int choice;

double num1, num2, result;

printf("Welcome to the Scientific Calculator\n");

printf("===================================\n");

printf("Choose an operation:\n");

printf("1. Addition\n");

printf("2. Subtraction\n");

printf("3. Multiplication\n");

printf("4. Division\n");

printf("5. Exponentiation\n");

printf("6. Square Root\n");

printf("7. Logarithm (base 10)\n");

printf("8. Exit\n");

scanf("%d", &choice);

while(choice != 8) {

switch(choice) {

case 1:

printf("Enter the first number: ");

scanf("%lf", &num1);

printf("Enter the second number: ");

scanf("%lf", &num2);

result = num1 + num2;

printf("Result: %.3lf\n", result);

break;

case 2:

printf("Enter the first number: ");

scanf("%lf", &num1);

printf("Enter the second number: ");

scanf("%lf", &num2);

result = num1 - num2;

printf("Result: %.3lf\n", result);

break;

case 3:

printf("Enter the first number: ");

scanf("%lf", &num1);

printf("Enter the second number: ");

scanf("%lf", &num2);

result = num1 * num2;

printf("Result: %.3lf\n", result);

break;

case 4:

printf("Enter the first number: ");

scanf("%lf", &num1);

printf("Enter the second number: ");

scanf("%lf", &num2);

result = num1 / num2;

printf("Result: %.3lf\n", result);

break;

case 5:

printf("Enter a base number: ");

scanf("%lf", &num1);

printf("Enter an exponent: ");

scanf("%lf", &num2);

result = pow(num1, num2);

printf("Result: %.3lf\n", result);

break;

case 6:

printf("Enter a number to take the square root of: ");

scanf("%lf", &num1);

result = sqrt(num1);

printf("Result: %.3lf\n", result);

break;

case 7:

printf("Enter a number to take the logarithm of: ");

scanf("%lf", &num1);

result = log10(num1);

printf("Result: %.3lf\n", result);

break;

default:

printf("Invalid choice, please try again.\n");

}

printf("\n");

printf("Choose an operation:\n");

printf("1. Addition\n");

printf("2. Subtraction\n");

printf("3. Multiplication\n");

printf("4. Division\n");

printf("5. Exponentiation\n");

printf("6. Square Root\n");

printf("7. Logarithm (base 10)\n");

printf("8. Exit\n");

scanf("%d", &choice);

}

printf("Thanks for using the Scientific Calculator!\n");

return 0;

}

Program Description:

We begin by including the necessary header files. The stdio.h header file provides input and output functions, the stdlib.h header file provides standard library functions, and the math.h header file provides mathematical functions such as sqrt() and pow().

We declare our variables, including choice which stores the user's menu choice, num1 and num2 which store the numbers to be used in the calculation, and result which stores the result of the calculation.

We display the welcome message and the available operations. We then prompt the user to choose an operation by entering a number from 1 to 8. We use scanf() to read the user's input and store it in the variable choice.

Next, we have a while loop that runs as long as the user does not choose option 8, which is to exit the program. Inside the loop, we have a switch statement that performs the appropriate operation based on the user's choice. The switch statement uses the value of the choice variable to determine which case to execute.

Each case prompts the user for the necessary input values and performs the appropriate calculation using built-in functions from the math library. The result is then printed to the console.

If the user enters an invalid choice, the default case is executed, which simply prints an error message and prompts the user to try again.

After each operation is performed, the user is prompted again to choose another operation or to exit the program. This loop continues until the user chooses option 8, at which point the program exits.

Finally, a thank you message is printed to the console.

Conclusion:

This program provides a simple and user-friendly interface for performing a variety of mathematical operations. It is also easily extensible, as new operations can be added by simply adding a new case to the switch statement.