Calculator program in C ||Simple calculator program in C || Free Code Center

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:

  1. The code starts by including the standard input/output library (stdio.h) which provides functions for input and output operations.
  2. The main() function is the entry point of the program.
  3. It declares a character variable called operator to store the user’s choice of operation.
  4. 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.
  5. The scanf() function is used to read the character input from the user and store it in the operator variable.
  6. The program declares three variables: first, second, and area. These variables are of type double to store decimal numbers.
  7. The program prompts the user to enter two numbers one by one using the printf() function.
  8. The scanf() function is used again to read two decimal numbers from the user and store them in the first and second variables.
  9. The program uses a series of if and else 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 of first and second using the + operator and prints the result using printf().
    • If the operator is -, it calculates the subtraction of second from first using the - operator and prints the result.
    • If the operator is *, it calculates the multiplication of first and second using the * operator and prints the result.
    • If the operator is /, it calculates the division of first by second using the / operator and prints the result.
    • If the operator is a, it calculates the area of a circle using the formula 3.1416 * first * second and stores the result in the area variable. Then it prints the area of the circle.
    • If the operator is b, it calculates the circumference of a circle using the formula 2.0 * 3.1416 * first and stores the result in the area variable. Then it prints the circumference.
    • If the operator is c, it calculates the area of a rectangle using the formula first * second and stores the result in the area variable. Then it prints the area of the rectangle.
  10. The code does not handle cases where the user enters an invalid operator.
  11. The main() function does not have a return 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.

How do you use Scanf to enter integers into the array in C?

Beecrowd1009 – Salary with Bonus solution with C

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
NEW TOXIC ROCKET CYCLE DECK in Clash Royale 2023 Best Deck in Clash Royale Best Deck for Arena 8 in Clash Royale Best Hog deck for Pro players Best Clash Royale Deck