Breaking

Monday, July 9, 2018

How To Create A Basic Calculator Program With Switch Statement.


In this Post you will learn about creating a basic calc program , here not only you will learn but also you can get the code from here. Follow The Steps.

--Program Code--


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

int main()
{
    int number1,number2;  //Integer Variable

    float result; //Float Variable
    char ch;     //Char Variable
    clrscr(); //To Clear The Previous Screen

    printf("Enter first number : ");
    scanf("%d",&number1); //To Store The First Value.
    printf("Enter second number : "); 
    scanf("%d",&num2); //To Store The Second Value.
     
    printf("Choose operation to perform (+,-,*,/,%): ");
    scanf(" %c",&ch); //To Store The Operator.
     
    result=0;
    switch(ch)    
    {
        case '+':
            result=num1+num2;
            break;
             
        case '-':
            result=num1-num2;
            break;
         
        case '*':
            result=num1*num2;
            break;
             
        case '/':
            result=(float)num1/(float)num2;
    break;
             
        case '%':
            result=num1%num2;
            break;
        default:
            printf("Invalid operation.\n");
    }

    printf("Result: %d %c %d = %f\n",num1,ch,num2,result); //To Print The Result.
    return 0;
}


--Output--




1 comment: