High-level programming languages like C are widely used in computer programming. In the early 1970s, Dennis Ritchie invented it at Bell Labs for system programming. The C programming language is a procedural language, which means every command is executed one by one.
Especially in systems programming, such as operating systems, device drivers, and embedded systems, C has the advantage of interacting with the hardware of the computer. Furthermore, it lets programmers manipulate memory and access system-level data, so they can make their code faster and more efficient. So through this article, we provide the best Simple Calculator Program In C.
What is C Language?
C is a compiled language, meaning that the code is translated into machine-readable instructions by a compiler before being executed by the computer. This makes C programs faster and more efficient than interpreted languages, such as Python or JavaScript.
Also check: Calculator Program In Javascript
C has influenced many other programming languages, including C++, Java, and Python, and is still widely used today in areas such as software development, game development, and operating system design. Its popularity can be attributed to its simplicity, portability, and flexibility, which make it an ideal choice for a wide range of programming tasks.
Basic Calculator Program in C
simple C program for a basic calculator that can perform addition, subtraction, multiplication, and division:
#include <stdio.h>
// Function to add two numbers
double add(double a, double b) {
return a + b;
}
// Function to subtract two numbers
double subtract(double a, double b) {
return a – b;
}
// Function to multiply two numbers
double multiply(double a, double b) {
return a * b;
}
// Function to divide two numbers
double divide(double a, double b) {
// Check for division by zero
if (b != 0) {
return a / b;
} else {
printf(“Error: Division by zero.\n”);
return 0;
}
}
int main() {
char operator;
double num1, num2, result;
// Input for the operator
printf(“Enter operator (+, -, *, /): “);
scanf(” %c”, &operator);
// Input for two numbers
printf(“Enter two numbers: “);
scanf(“%lf %lf”, &num1, &num2);
// Perform the calculation based on the 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;
default:
printf(“Error: Invalid operator.\n”);
return 1; // Exit with an error code
}
// Display the result
printf(“Result: %.2lf\n”, result);
return 0;
}
Calculator program in C using Loop
The program uses loops to continue to accept user input until the user decides to exit the program. In the main function, the program first declares variables for the operator, two numbers, and the result. Then, the program asks the user to enter an operator and two numbers using the printf() and scanf() functions.
Next, the program uses a switch statement to operate based on the user’s operator input. The switch statement is used to perform the calculation based on the operator entered by the user. The result of the calculation is stored in the variable ‘result’.
Finally, the program prints out the result using printf() function.
#include <stdio.h>
int main() {
char operator;
float num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator\n");
return 0;
}
printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
return 0;
}
Calculator program in C using if else
In this program, the user is prompted to enter an operator (+, -, *, /) and two numbers. The program then uses if-else statements to determine which operation to perform based on the operator entered by the user. If the operator is invalid, the program will print an error message and exit.
For example, if the user enters + for the operator and 2 and 3 for the numbers, the program will output:
2.00 + 3.00 = 5.00
This program can be extended to include additional operations or features, such as looping to allow for multiple calculations, error handling for invalid input, and support for more advanced mathematical functions.
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
if (operator == '+') {
result = num1 + num2;
}
else if (operator == '-') {
result = num1 - num2;
}
else if (operator == '*') {
result = num1 * num2;
}
else if (operator == '/') {
result = num1 / num2;
}
else {
printf("Invalid operator\n");
return 1;
}
printf("%.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);
return 0;
}
Complex calculator code in C
This code creates a complex number struct with real and imaginary parts, and then defines functions for performing addition, subtraction, multiplication, and division on these complex numbers.
In the main()
function, the user is prompted to input two complex numbers and select an operation to perform. The appropriate function is then called based on the user’s choice.
The add()
function takes two complex numbers a
and b
and adds their real and imaginary parts separately to get the result. The same goes for the subtract()
function.
The multiply()
function takes two complex numbers a
and b
and multiplies them according to the formula (a + bi) * (c + di) = (ac – bd) + (ad + bc)i.
The divide()
function takes two complex numbers a
and b
and divides them according to the formula (a + bi) / (c + di) = ((ac + bd) / (c^2 + d^2)) + ((bc – ad) / (c^2 + d^2))i.
Once the operation is performed, the result is printed to the console.
#include <stdio.h>
#include <math.h>
struct complex {
float real;
float imag;
};
void add(struct complex a, struct complex b) {
float real_result = a.real + b.real;
float imag_result = a.imag + b.imag;
printf("Result: %.2f + %.2fi\n", real_result, imag_result);
}
void subtract(struct complex a, struct complex b) {
float real_result = a.real - b.real;
float imag_result = a.imag - b.imag;
printf("Result: %.2f + %.2fi\n", real_result, imag_result);
}
void multiply(struct complex a, struct complex b) {
float real_result = a.real * b.real - a.imag * b.imag;
float imag_result = a.real * b.imag + a.imag * b.real;
printf("Result: %.2f + %.2fi\n", real_result, imag_result);
}
void divide(struct complex a, struct complex b) {
float real_result = (a.real * b.real + a.imag * b.imag) / (b.real * b.real + b.imag * b.imag);
float imag_result = (a.imag * b.real - a.real * b.imag) / (b.real * b.real + b.imag * b.imag);
printf("Result: %.2f + %.2fi\n", real_result, imag_result);
}
int main() {
struct complex num1, num2;
int choice;
printf("Enter the real and imaginary parts of the first complex number: ");
scanf("%f %f", &num1.real, &num1.imag);
printf("Enter the real and imaginary parts of the second complex number: ");
scanf("%f %f", &num2.real, &num2.imag);
printf("Select operation:\n");
printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add(num1, num2);
break;
case 2:
subtract(num1, num2);
break;
case 3:
multiply(num1, num2);
break;
case 4:
divide(num1, num2);
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Calculator Program In C Code
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator\n");
return 1;
}
printf("%.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);
return 0;
}
This program takes an operator (+, -, *, /) and two operands as input, performs the desired arithmetic operation, and outputs the result. The switch
statement is used to select the appropriate operation based on the input operator. The printf
function is used to display the result in a formatted manner.
This is a very basic calculator program that can be expanded upon to include more complex operations and functionality. It serves as a good starting point for anyone looking to learn C programming and develop their own calculator application. Those students who want use or download the Calculator program in C PDF.
Calculator Program in C Sharp
This program prompts the user to enter two numbers and an operation (+, -, *, /). It then performs the corresponding arithmetic operation and displays the result. The program also includes error handling for division by zero.
using System;
class Calculator
{
static void Main()
{
double num1, num2, result;
char operation;
Console.WriteLine(“Simple Calculator Program\n”);
Console.Write(“Enter the first number: “);
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write(“Enter the second number: “);
num2 = Convert.ToDouble(Console.ReadLine());
Console.Write(“Enter the operation (+, -, *, /): “);
operation = Convert.ToChar(Console.ReadLine());
switch (operation)
{
case ‘+’:
result = num1 + num2;
Console.WriteLine($”Result: {num1} + {num2} = {result}”);
break;
case ‘-‘:
result = num1 – num2;
Console.WriteLine($”Result: {num1} – {num2} = {result}”);
break;
case ‘*’:
result = num1 * num2;
Console.WriteLine($”Result: {num1} * {num2} = {result}”);
break;
case ‘/’:
if (num2 != 0)
{
result = num1 / num2;
Console.WriteLine($”Result: {num1} / {num2} = {result}”);
}
else
{
Console.WriteLine(“Error: Division by zero is not allowed.”);
}
break;
default:
Console.WriteLine(“Error: Invalid operation entered.”);
break;
}
Console.ReadLine();
}
}
How do you program a calculator in C?
Programming a calculator in C involves writing code that can perform mathematical calculations based on user input. Here’s a simple example of how you can create a basic calculator in C that can perform addition, subtraction, multiplication, and division:
#include <stdio.h>
int main() {
char operator;
double num1, num2;
// Prompt the user for input
printf(“Enter an operator (+, -, *, /): “);
scanf(“%c”, &operator);
printf(“Enter two numbers: “);
scanf(“%lf %lf”, &num1, &num2);
// Perform the calculation based on the operator
switch (operator) {
case ‘+’:
printf(“Result: %.2lf\n”, num1 + num2);
break;
case ‘-‘:
printf(“Result: %.2lf\n”, num1 – num2);
break;
case ‘*’:
printf(“Result: %.2lf\n”, num1 * num2);
break;
case ‘/’:
if (num2 == 0) {
printf(“Error: Division by zero is not allowed.\n”);
} else {
printf(“Result: %.2lf\n”, num1 / num2);
}
break;
default:
printf(“Error: Invalid operator\n”);
}
return 0;
}
In this code:
- We include the “<stdio.h>” header to use input and output functions.
- We declare variables to store the operator, as well as the two numbers provided by the user.
- We prompt the user to enter an operator (+, -, *, /) and two numbers.
- We use a switch statement to perform the appropriate calculation based on the operator entered by the user. If the user enters an invalid operator or attempts to divide by zero, appropriate error messages are displayed.
- The “printf” statements are used to display the result of the calculation.
- The program ends with “return 0;”.
Compile and run this code, and it will function as a basic calculator. You can expand upon this example by adding more functionality or implementing a graphical user interface (GUI) for a more user-friendly calculator.
Leave a Reply