[C언어] if statement 이용한 계산기 (if문 계산기)


Now I am going to code to make a calculator that using if statements.( e.g. if, else if, else, but not switch)


#include <stdio.h>

int main(void){

int num1, num2=0;

char oper =0;


printf("This is a calculator using if statements\n");

printf("Please input one operator you want to calculate\n");

scanf("%c", &oper);


{if(oper=='+'){

printf("Now we help you to calculate 'addition'\n");

printf("please input two numbers you want to calculate : ");

scanf("%d %d", &num1, &num2);

printf("addition result is num1+num2 = %d", num1+num2);

} else if ( oper=='-'){

printf("Now we help you to calculate 'subtraction'\n");

printf("please input two numbers you want to calculate : ");

scanf("%d %d", &num1, &num2);

printf("addition result is num1-num2 = %d", num1-num2);

} else if ( oper == '*'){

printf("Now we help you to calculate 'multiplication'\n");

printf("please input two numbers you want to calculate : ");

scanf("%d %d", &num1, &num2);

printf("addition result is num1*num2 = %d", num1*num2);

} else if (oper =='/'){

printf("Now we help you to calculate 'division'\n");

printf("please input two numbers you want to calculate : ");

scanf("%d %d", &num1, &num2);

printf("addition result is num1-num2 = %d", num1-num2);

} else {

printf("You typed wrong operator\n");

}

}

  return 0;

}


+ Recent posts