Solution 6 to 12
Solution 8

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

  1. Start
  2. Initialize Variables
    • float x;
    • int choice;
    • long f;
  3. Display Menu
    • Show available operations.
  4. Accept User Choice
    • Read user input for the choice.
  5. Check Choice Validity
    • If choice is valid (1-9), proceed.
    • If choice is 9, exit the program.
  6. Prompt for Value
    • Read the value of x from the user.
  7. 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.
  8. Display Result
    • Print the result of the calculation.
  9. Repeat
    • Go back to the menu for another operation.
  10. 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