calculator in c program

Calculator using switch Statement  :





This program takes an arithmetic operator +, -, *, / and two operands from the user and performs the calculation on the two operands depending upon the operator entered by the user.



program code: 


#include<stdio.h>
int main()
{
    float sum, mul,div, a,b,result;
    char op;
    printf("    Calculator  \n");
    printf("Enter the fist number : ");
    scanf("%f",&a);
    printf("Enter the operator ");
    scanf(" %c",&op);
    printf("Enter the 2nd number : ");
    scanf("%f",&b);
    switch(op)
    {
    case '+':
        result=a+b;
        printf("result = %.2f",result);
        break;

    case '-':
            result=a-b;
        printf("result = %.2f",result);
        break;

    case '*':
            result=a*b;
        printf("result = %.3f",result);
        break;

    case '/':
            result=a/b;
        printf("result = %.3f",result);
        break;

         default:
        printf("the operator is  not valid\n");

    }



    return 0;

}



Output : 










or program code  ( EOF file)  




#include<stdio.h>
int main()
{
    float sum, mul,div, a,b,result;
    char op;
    printf("    Calculator  \n");
    printf("Enter the fist number : ");
    while (scanf("%f",&a)!=EOF)
    {
        printf("Enter the operator ");
        scanf(" %c",&op);
        printf("Enter the 2nd number : ");
        scanf("%f",&b);
        switch(op)
        {
        case '+':
            result=a+b;
            printf("result = %.2f\n",result);
            printf("Enter the fist number : ");
            break;

        case '-':
            result=a-b;
            printf("result = %.2f\n",result);
            printf("Enter the fist number : ");
            break;

        case '*':
            result=a*b;
            printf("result = %.3f\n",result);
            printf("Enter the fist number : ");
            break;

        case '/':
            result=a/b;
            printf("result = %.3f\n",result);
            break;

        default:
            printf("the operator is  not valid\n");
            printf("Enter the fist number : ");

        }
    }



    return 0;

}


No comments

Powered by Blogger.