- Setup Environment
- Set Up Your Development Environment
- Create a New C File
- Step by step code instruction
- Include Necessary Header Files
- Define Function Prototypes
- Implement Basic Arithmetic Functions
- Implement Additional Functions
- Write the Main Function
- Compile and Run
- Complete Source code
- Test and Refine
- Output
- Tags:
Great, Finally in our previous tutorial, we’ve already built a small Student management application using C programming.
Today, we’ll show a step-by-step guide on how to write a simple calculator application in the C programming language:
¶Setup Environment
¶Set Up Your Development Environment
Ensure you have a C compiler installed on your system. Popular choices include GCC for Unix-like systems (Linux, macOS) and MinGW for Windows. You can use an Integrated Development Environment (IDE) like Code::Blocks, Visual Studio, or simply use a text editor and the command line.
¶Create a New C File
Open your preferred text editor or IDE and create a new C file. Save it with a meaningful name, like calculator.c
.
¶Step by step code instruction
¶Include Necessary Header Files
In your C file, include the necessary header files for input/output and math functions:
#include <stdio.h>#include <math.h>
In here, stdio.h
is for standard input output andmath.h
is for math functions.
¶Define Function Prototypes
Declare the prototypes for the functions you will use later in the program. These will include functions for basic arithmetic operations and any additional operations you want to support:
float add(float num1, float num2);float subtract(float num1, float num2);float multiply(float num1, float num2);float divide(float num1, float num2);float modulus(float num1, float num2);float power(float num, float exponent);
¶Implement Basic Arithmetic Functions
Define functions for addition, subtraction, multiplication, and division. These functions will perform the respective operations:
float add(float num1, float num2) { return num1 + num2;}float subtract(float num1, float num2) { return num1 - num2;}float multiply(float num1, float num2) { return num1 * num2;}float divide(float num1, float num2) { if (num2 != 0) { return num1 / num2; } else { printf("Error: Division by zero\n"); return 0; }}
¶Implement Additional Functions
If you want to support modulus and power operations, implement those functions as well:
float modulus(float num1, float num2) { if (num2 != 0) { return fmod(num1, num2); } else { printf("Error: Modulus by zero\n"); return 0; }}float power(float num, float exponent) { return pow(num, exponent);}
¶Write the Main Function
In the main
function, prompt the user for input, read the numbers and operator, and perform the calculation:
int main() { char operator; float num1, num2, result; printf("Enter first number: "); scanf("%f", &num1); printf("Enter operator (+, -, *, /, %, ^): "); scanf(" %c", &operator); printf("Enter second number: "); scanf("%f", &num2); switch (operator) { case '+': result = add(num1, num2); break; case '-': result = subtract(num1, num2); break; case '*': result = multiply(num1, num2); break; case '/': result = divide(num1, num2); break; case '%': result = modulus(num1, num2); break; case '^': result = power(num1, num2); break; default: printf("Invalid operator\n"); return 1; } printf("Result: %f\n", result); return 0;}
¶Compile and Run
Save your file and compile it using your C compiler. Execute the resulting executable to test your calculator.
¶Complete Source code
#include <stdio.h>#include <math.h>// Function prototypesfloat add(float num1, float num2);float subtract(float num1, float num2);float multiply(float num1, float num2);float divide(float num1, float num2);float modulus(float num1, float num2);float power(float num, float exponent);int main() { char operator; float num1, num2, result; // Input printf("Enter first number: "); scanf("%f", &num1); printf("Enter operator (+, -, *, /, %, ^): "); scanf(" %c", &operator); printf("Enter second number: "); scanf("%f", &num2); // Perform calculation based on operator switch (operator) { case '+': result = add(num1, num2); break; case '-': result = subtract(num1, num2); break; case '*': result = multiply(num1, num2); break; case '/': result = divide(num1, num2); break; case '%': result = modulus(num1, num2); break; case '^': result = power(num1, num2); break; default: printf("Invalid operator\n"); return 1; // Exit with an error code } // Output the result printf("Result: %f\n", result); return 0; // Exit successfully}// Function to add two numbersfloat add(float num1, float num2) { return num1 + num2;}// Function to subtract two numbersfloat subtract(float num1, float num2) { return num1 - num2;}// Function to multiply two numbersfloat multiply(float num1, float num2) { return num1 * num2;}// Function to divide two numbersfloat divide(float num1, float num2) { if (num2 != 0) { return num1 / num2; } else { printf("Error: Division by zero\n"); return 0; // Return a default value in case of division by zero }}// Function to calculate modulusfloat modulus(float num1, float num2) { if (num2 != 0) { return fmod(num1, num2); } else { printf("Error: Modulus by zero\n"); return 0; // Return a default value in case of modulus by zero }}// Function to calculate powerfloat power(float num, float exponent) { return pow(num, exponent);}
¶Test and Refine
Test your calculator with various inputs to ensure it works as expected. Consider adding error checking and improving the user interface based on your requirements.
¶Output
Certainly! Here are sample outputs for various operators in the calculator application:
Addition Testing
Enter first number: 15Enter operator (+, -, *, /, %, ^): +Enter second number: 7Result: 22.000000
Subtraction Testing
Enter first number: 20Enter operator (+, -, *, /, %, ^): -Enter second number: 8Result: 12.000000
Multiplication Testing
Enter first number: 9Enter operator (+, -, *, /, %, ^): *Enter second number: 6Result: 54.000000
Division Testing
Enter first number: 36Enter operator (+, -, *, /, %, ^): /Enter second number: 4Result: 9.000000
Modulus Testing
Enter first number: 25Enter operator (+, -, *, /, %, ^): %Enter second number: 7Result: 4.000000
Exponentiation Testing
Enter first number: 2Enter operator (+, -, *, /, %, ^): ^Enter second number: 3Result: 8.000000
Congratulations! You’ve now created a simple calculator application in C. This basic calculator can be expanded and enhanced based on your needs and preferences.
¶Tags:
C programming calculator, Simple C calculator tutorial, C programming for beginners, Building a console calculator in C, Step-by-step C calculator guide, C programming math functions, Creating a calculator application in C, C programming language tutorial, Code example for a C calculator, Understanding C function prototypes, Input/output in C programming, C compiler setup for beginners, C development environment setup, User-friendly C calculator, Handling errors in C programming, C programming switch statement, C programming data types, C scanf function tutorial, Floating-point arithmetic in C, C programming conditional statements, C programming language basics, Arithmetic operations in C with functions, C calculator application demonstration, Building a basic calculator with C, Learn C programming interactively, Interactive C calculator example, Basic C coding for mathematical operations, C programming logic for calculator, Enhancing C calculator with additional functions