0% found this document useful (0 votes)
15 views1 page

Simple Calculator Program

This document contains a simple C program that implements a basic calculator. It allows users to perform addition, subtraction, multiplication, and division on two numbers. The program includes error handling for division by zero and invalid operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

Simple Calculator Program

This document contains a simple C program that implements a basic calculator. It allows users to perform addition, subtraction, multiplication, and division on two numbers. The program includes error handling for division by zero and invalid operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Simple Calculator in C

#include <stdio.h>

int main() {
char operator;
double num1, num2;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf", &num1, &num2);

switch(operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf", num1, num2, num1 * num2);
break;
case '/':
if(num2 != 0.0)
printf("%.2lf / %.2lf = %.2lf", num1, num2, num1 / num2);
else
printf("Division by zero is not allowed.");
break;
default:
printf("Invalid operator");
}

return 0;
}

You might also like