Basic calculator in c

 



In this code I used different variables for different-different calculations. First of all, I inputted the number first and second from the users and then the operator for a specific calculation like +, -, *, /, ^. Here I defined the work of variables and then I used the conditional statements for choosing right operator for calculations. In the condition the condition check that the character "op" is equal to operator inputted by users or not if true then it print the solution and if the user enter wrong operator or number, it will print "Incorrect Number or Operator, try again.".

Main Keywords and variables used in this code:

1.We included stdio.h, conio.h, and math.h file.
2.then, we use int keyword and main function to run this code.
3.then, we defined variables int and char.
4.then, we inputted numbers and operator.
5.then, we store the instructions or work in the individual variable.
6.then, we use if-else statements for printing right solution of the operator.
7.then, we use getch for viewing the result.
8.and then the code ends. 





If you'd like to use this code, feel free to copy it from here:

#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
    int x, y, sum, sub, mul, div, power;
    char op;

    printf("Enter the first number :");
    scanf("%d", &x);

    printf("Enter the second number :");
    scanf("%d", &y);

    printf("Enter the Operator (Addition (+),Subtraction (-),Multiplication (*),Divide (/),Power (^)) :");
    scanf(" %c", &op);

    sum = x + y;
    sub = x - y;
    mul = x * y;
    div = x / y;
    power = pow(x, y);
    if (op == '+')
    {
        printf("\nAnswer : %d", sum);
    }
    else if (op == '-')
    {
        printf("\nAnswer : %d", sub);
    }
    else if (op == '*')
    {
        printf("\nAnswer : %d", mul);
    }
    else if (op == '/')
    {
            printf("\nAnswer : %d", div);
    }
    else if (op == '^')
    {
        printf("\nAnswer : %d", power);
    }
    else
    {
        printf("Incorrect Number or Operator, try again.");
    }
   
   
   
   
   
    getch();
}

Comments

Popular posts from this blog

Switch-Case Magic: Checking Vowels and Consonants in C