Pages

Monday, 29 January 2018

Calculator (Calls Functions to Do Operations) in C Language


Calculator (Calls Functions to Do Operations)
/* A simple calculator which calls function to do operations*/
#include <stdio.h>
#include <conio.h>
/*declaring function*/
float add(float,float);
float sub(float,float);
float mult(float,float);
float div(float,float);

int main()
{
    char exit = ' ';
    do
        {
              
            float dig1 = 0 , dig2 = 0 ;
            char opert;
            printf("Please enter first digit: ");
            scanf("%f",&dig1);
            printf("\n\nNow enter operation to perform: ");
            scanf("%c",&opert);
            opert=getche();
                 
                   /*if operation is division then implementing cheks and conditions to stop entering denominator as zero*/
            if (opert=='/')
            {
                while(dig2 == 0)
                {
                printf("\n\n\nNow enter 2nd the digit: ");
                scanf("%f",&dig2);
                    if(dig2==0)
                    {
                               printf("\tWarning!\nDenominator can not be zero.");
                    }
                }
            }
            else
            {
                printf("\n\n\nNow enter 2nd the digit: ");
                scanf("%f",&dig2);
            }
                          
            /*Checking operation and calling function related to operation*/      
            switch (opert)
            {
                case '+':
                add(dig1,dig2);
                break;
                case '-':
                sub(dig1,dig2);
                break;
              
                case '*':   
                mult(dig1,dig2);
                break;
                               
                case '/':   
                div(dig1,dig2);
                break;
                default :
                printf("\t\n\nWarning!\n Wrong operation entered.");
            }
            exit = ' ';
            /*To continue or exit*/
            while(exit !='e' && exit != 'c' && exit !='E' && exit != 'C')
            {
                printf("\n\nEnter 'e' to exit or 'c' to continue using program: ");
                scanf("%c",&exit);
                exit = getche();
                printf("\n\n");
            }
        }
        while (exit != 'e' && exit != 'E');  
  

    getche();
    return 0;
}


/*Defining functions*/
/*addition function*/
float add(float digi1add, float digi2add )
{
    printf("\n\n%f + %f = %f\n\n\n",digi1add, digi2add, digi1add + digi2add);
}

/*subtraction function*/
float sub(float digi1sub, float digi2sub )
{
    printf("\n\n%f - %f = %f\n\n\n",digi1sub, digi2sub, digi1sub - digi2sub);
}

/*multiplication function*/
float mult(float digi1mul, float digi2mult )
{
     printf("\n\n%f x %f = %f\n\n\n",digi1mul, digi2mult, digi1mul * digi2mult );
}

/*division function*/
float div(float digi1div, float digi2div )
{
    
    printf("\n\n%f / %f = %f\n\n\n",digi1div, digi2div, digi1div / digi2div);
}

do-while loop (prints counting)