Calculator program in C
#include <stdio.h> int main () { char operator; printf("enter the operator (+ - * /)\n press A for Area of circle\n press B for circumference of circle \n press C area of rectangle\n"); scanf("%c",&operator); double first,second,area; printf("enter two numbers one by one= "); scanf("%lf %lf",&first,&second); if (operator=='+') { printf("The sum is=%.3lf",first+second); } else if(operator=='-'){ printf("The subtration is=%.3lf",first-second); } else if(operator=='*'){ printf("The multiplication is=%.3lf",first*second); } else if(operator=='/'){ printf("The division is=%.3lf",first/second); } else if(operator=='a'){ area =3.1416*first*second; printf("Area of circle is%.3lf",area); } else if(operator=='b') { area =2.0*3.1416*first; printf("circumference of circleif is%.3lf",area); } else if(operator =='c'){ area=first*second; printf("area of rectangle is%.3lf",area); } }
Calculator program in C code explanation
This code is a C program that performs basic arithmetic operations and calculates the area of a circle and rectangle based on user input. Let’s go through the code step by step:
- The code starts by including the standard input/output library (
stdio.h
) which provides functions for input and output operations. - The
main()
function is the entry point of the program. - It declares a character variable called
operator
to store the user’s choice of operation. - The program prompts the user to enter the operator choice by displaying a menu of options, including options for the area of a circle, circumference of a circle, and area of a rectangle.
- The
scanf()
function is used to read the character input from the user and store it in theoperator
variable. - The program declares three variables:
first
,second
, andarea
. These variables are of typedouble
to store decimal numbers. - The program prompts the user to enter two numbers one by one using the
printf()
function. - The
scanf()
function is used again to read two decimal numbers from the user and store them in thefirst
andsecond
variables. - The program uses a series of
if
andelse if
statements to determine the operator entered by the user and perform the corresponding operation or calculation.- If the operator is
+
, it calculates the sum offirst
andsecond
using the+
operator and prints the result usingprintf()
. - If the operator is
-
, it calculates the subtraction ofsecond
fromfirst
using the-
operator and prints the result. - If the operator is
*
, it calculates the multiplication offirst
andsecond
using the*
operator and prints the result. - If the operator is
/
, it calculates the division offirst
bysecond
using the/
operator and prints the result. - If the operator is
a
, it calculates the area of a circle using the formula3.1416 * first * second
and stores the result in thearea
variable. Then it prints the area of the circle. - If the operator is
b
, it calculates the circumference of a circle using the formula2.0 * 3.1416 * first
and stores the result in thearea
variable. Then it prints the circumference. - If the operator is
c
, it calculates the area of a rectangle using the formulafirst * second
and stores the result in thearea
variable. Then it prints the area of the rectangle.
- If the operator is
- The code does not handle cases where the user enters an invalid operator.
- The
main()
function does not have areturn
statement, so it implicitly returns 0, indicating successful execution of the program.
Overall, this program allows the user to perform basic arithmetic operations and calculate the area of a circle or rectangle based on their choice of operator.