Implementing a Scientific Calculator in C Language
In this article, we will learn how to implement a simple scientific calculator using the C language. This calculator will support basic mathematical operations such as addition, subtraction, multiplication, and division, as well as advanced operations like exponentiation and square root.
Target Features
- Support basic operations (addition, subtraction, multiplication, division)
- Support exponentiation
- Support square root calculation
- User-friendly interface to prompt user input
Before You Start
Make sure you have a C compiler installed in your development environment, such as GCC. If you do not have such an environment, you can choose to use an online compiler or install a local IDE, such as Code::Blocks or Dev-C++.
Program Structure
The entire program can be divided into the following parts:
- Header File Inclusion: Include the necessary library files.
- Function Declarations: Define the various functional functions we will use.
- Main Function: The entry point of the program, handling user input and calling the corresponding functional functions.
- Function Implementation: Implement the code logic corresponding to each mathematical operation.
Below is the complete code example:
#include <stdio.h>
#include <math.h>
// Function declarations
void add(double a, double b);
void subtract(double a, double b);
void multiply(double a, double b);
void divide(double a, double b);
void power(double base, double exponent);
void square_root(double number);
int main() {
int choice;
double num1, num2;
printf("Welcome to the Scientific Calculator!\n");
// Provide menu options to the user
printf("Please select 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");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Please enter two numbers:\n");
scanf("%lf %lf", &num1, &num2);
add(num1, num2);
break;
case 2:
printf("Please enter two numbers:\n");
scanf("%lf %lf", &num1, &num2);
subtract(num1, num2);
break;
case 3:
printf("Please enter two numbers:\n");
scanf("%lf %lf", &num1, &num2);
multiply(num1, num2);
break;
case 4:
printf("Please enter two numbers (dividend and divisor):\n");
scanf("%lf %lf", &num1, &num2);
if (num2 != 0) {
divide(num1, num2);
} else {
// Prevent division by zero error
printf("Error: Divisor cannot be zero.\n");
}
break;
case 5:
// Exponentiation requires one base and one exponent
double base, exponent;
printf("\n Enter base and exponent: ");
scanf("%lf %lf", &base, &exponent);
power(base, exponent);
break;
case 6:
// Square root only requires one input
double number;
printf("\n Enter number for square root: ");
scanf("%lf", &number);
square_root(number);
break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}
// Addition function implementation
void add(double a, double b) {
printf("Result: %.2f\n", a + b);
}
// Subtraction function implementation
void subtract(double a, double b) {
printf("Result: %.2f\n", a - b);
}
// Multiplication function implementation
void multiply(double a, double b) {
printf("Result: %.2f\n", a * b);
}
// Division function implementation
void divide(double a, double b) {
printf("Result: %.2f\n", a / b);
}
// Power function implementation
void power(double base, double exponent) {
printf("Result: %.2f\n", pow(base, exponent));
}
// Square root function implementation
void square_root(double number) {
printf("Result: %.2f\n", sqrt(number));
}
Function Details
Addition (<span>add</span>)
This function takes two parameters and returns their sum. It uses the simple<span>+</span> operator for addition.
Subtraction (<span>subtract</span>)
Similarly, it takes two inputs and returns their difference.
Multiplication (<span>multiply</span>)
This function multiplies two input values and prints the result using the<span>*</span> operator.
Division (<span>divide</span>)
Before performing division, we must effectively check if we are trying to divide by zero, which is a common error. Division will only be executed if the second parameter is not zero; otherwise, an error message will be printed.
Exponentiation (<span>power</span>)
This uses the<span>pow()</span> method from the C standard library, which produces output based on the provided base and exponent.
Square Root (<span>square_root</span>)
This function takes one input and calculates the square root using the<span>sqrt()</span> method.
That concludes our basic scientific calculator implementation in C. I hope this article is helpful to everyone. If you have any questions about the code, feel free to leave a comment for discussion.