프로그래밍언어/C

[c] switch문을 이용한 간단한 계산기

dan2el 2021. 8. 25. 22:06
#include <stdio.h>

int main(void){

    int n;
    float a,b;
    float input,output;


    printf("It is a four arithmetic calculator. Please enter a number.\n\n");
    printf("1. Plus 2. Subtraction 3.Multiplication 4.Division\n\n");

    scanf("%d",&n);
    
    switch(n){
	case 1:
	    printf("Please enter two numbers to add.\n");
	    
	    scanf("%f %f",&a,&b);
	    
	    output = a+b;

	    printf("= %f",output);
	    
	    break;

	case 2:
	    printf("Please enter two numbers to substracted.\n");
	    
	    scanf("%f %f",&a,&b);
	    
	    output = a-b;

	    printf("= %f",output);

	    break;

	case 3:
	    printf("Please enter two numbers to Multiply.\n");
	    
	    scanf("%f %f",&a,&b);
	    
	    output = a*b;

	    printf("= %f",output);

	    break;

	case 4:
	    printf("Please enter two numbers to divide.\n");
	    
	    scanf("%f %f",&a,&b);
	    
	    output = a/b;

	    printf("= %f",output);

	    break;
    }
    
    return 0;

}

 실행결과

 

더하기

빼기

곱하기

나누기