Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

how to create calculator with switch in c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a,b,s;
    char d;

    printf("choose an operation btwn + - * /  : ");
    scanf("%c",&d);
    switch(d){
    case '+' :
        printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a + b ;
        printf("%.1f + %.1f = %.1f",a,b,s);
        break;
    case '-' :
        printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a - b ;
        printf("%.1f - %.1f = %.1f",a,b,s);
        break;
    case '*' :
         printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a * b ;
        printf("%.1f * %.1f = %f",a,b,s);
        break;
    case '/' :
         printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a / b ;
        printf("%.1f / %.1f = %.1f",a,b,s);
        break;
    default :
        printf("enter a valid operation !");
        break;
    }
    return 0;
}
 
PREVIOUS NEXT
Tagged: #create #calculator #switch
ADD COMMENT
Topic
Name
9+5 =