Skip to content

About

CLI scientific calculator in C: math & trig

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Scientific Calculator (C Program)

A simple command-line calculator written in C that supports two modes of operation:

Arithmetic Mode — basic math operations Trigonometry Mode — trigonometric functions (angle input in degrees)

Features

Mode 1: Arithmetic

Supports the following operators:

OperatorOperationNotes+Addition-Subtraction*Multiplication/DivisionHandles division-by-zero error^ExponentiationUses pow() from math.h%ModuloInteger modulo; handles zero divisor

Mode 2: Trigonometry

Supports the following functions (input angle is in degrees, automatically converted to radians):

FunctionDescriptionNotessinSinecosCosinetanTangentUndefined when cos(angle) = 0cosecCosecant (1/sin)Undefined when sin(angle) = 0secSecant (1/cos)Undefined when cos(angle) = 0cotCotangent (1/tan)Undefined when tan(angle) = 0

How to Compile

This program uses the math.h library, so you must link the math library when compiling with GCC:

bashgcc calculator.c -o calculator -lm

Note: The -lm flag is required on Linux/Unix systems to link the math library. It may not be needed on some Windows compilers (e.g., MinGW sometimes links it automatically).

How to Run

bash./calculator

Usage Example

Arithmetic Mode

Enter one for Arithmetic operation Enter two for Trigonometry Enter operation (one or two): one Enter the operator (+ - * / % ^): + Enter the first number: 10 Enter the second number: 5

Result: 15.0000

Trigonometry Mode

Enter one for Arithmetic operation Enter two for Trigonometry Enter operation (one or two): two Enter the value (in degrees): 30 Enter the function (sin, cos, tan, cosec, sec, cot): sin

sin(30.00) = 0.5000

Input Format

Mode selection: type one or two (case-sensitive) Arithmetic operator: a single character — +, -, *, /, ^, or % Numbers: decimal values (double), e.g. 10, 3.5, -2.75 Trigonometric function name: one of sin, cos, tan, cosec, sec, cot (lowercase)

Error Handling

The program checks for and reports the following invalid inputs:

Division by zero (/) Modulo by zero (%) Unknown/unsupported arithmetic operator Undefined trigonometric results (e.g., tan(90°), cosec(0°), cot(0°)) Unknown trigonometric function name Invalid mode selection (anything other than one or two)

Requirements

A C compiler (GCC recommended) Standard C libraries: stdio.h, string.h, math.h

Limitations / Possible Improvements

Mode and function names are case-sensitive (One or SIN will not be recognized). Only one calculation per run — the program exits after computing a result. Could be extended with a loop for repeated calculations. Inverse trig functions (asin, acos, atan) are not currently supported. No input validation for non-numeric entries (e.g., entering letters where a number is expected may cause undefined behavior).

License

Free to use, modify, and distribute for educational purposes.

About

CLI scientific calculator in C: math & trig

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages