Scientific Calculator
This C program implements a basic scientific calculator using the console. The calculator offers various mathematical functions including trigonometric functions (sin, cos, tan), logarithmic functions (natural log, base-10 log), exponentiation (e^x), square root, and factorial calculation. The user is presented with a menu to choose the desired operation. Depending on the choice, the program performs the corresponding calculation using the input value and displays the result. The program continues to run until the user chooses to exit by selecting the appropriate option from the menu.
Code Breakdown
Algorithm
- Start
- Initialize Variables
float x;
int choice;
long f;
- Display Menu
- Show available operations.
- Accept User Choice
- Read user input for the choice.
- Check Choice Validity
- If choice is valid (1-9), proceed.
- If choice is 9, exit the program.
- Prompt for Value
- Read the value of
x
from the user.
- Read the value of
- Perform Calculation Based on Choice
- Use
switch
statement to execute the selected operation:- 1: Calculate
sin(x)
in degrees. - 2: Calculate
cos(x)
in degrees. - 3: Calculate
tan(x)
in degrees. - 4: Calculate
log(x)
(natural logarithm). - 5: Calculate
log10(x)
(base-10 logarithm). - 6: Calculate
e^x
. - 7: Calculate
sqrt(x)
. - 8: Calculate factorial of
x
.
- 1: Calculate
- Use
- Display Result
- Print the result of the calculation.
- Repeat
- Go back to the menu for another operation.
- End
Code Explanation
Example Flowchart
Start
|
V
Display Menu Options
1. Sin(x) 2. Cos(x) 3. Tan(x)
4. Logn(x) 5. Log10(x) 6. e^x
7. Squareroot(x) 8. x! 9. Exit
|
V
Get User Choice (1-9)
/ | | | | | | | | \
/ | | | | | | | \
/ | | | | | | | \
/ | | | | | | | \
Choice < 1 or Choice > 9 Choice == 9
/ \
/ \
Continue Loop Exit Program
|
V
Prompt for x
|
V
Perform Calculation
|
+--------------------------+
| |
V V
Case 1: Sin(x) Case 2: Cos(x)
| |
V V
Print Result Print Result
|
V
End of Calculation
|
+--------------------------+
| |
V V
Case 3: Tan(x) Case 4: Logn(x)
| |
V V
Print Result Print Result
|
V
End of Calculation
|
+--------------------------+
| |
V V
Case 5: Log10(x) Case 6: e^x
| |
V V
Print Result Print Result
|
V
End of Calculation
|
+--------------------------+
| |
V V
Case 7: Squareroot(x) Case 8: x!
| |
V V
Print Result Calculate Factorial
| |
V V
Print Result Print Result
|
V
End of Calculation
|
V
End