[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;
}
'Study > C Programming' 카테고리의 다른 글
[C언어] while loop / while statement / 자연수 더하기 (0) | 2018.08.01 |
---|---|
[C언어] for loop / for statement / 자연수 더하기 (0) | 2018.08.01 |
[C언어] if~else와 switch 비교 (0) | 2018.08.01 |
[C언어] 최대공약수, 최소공배수 구하기 (0) | 2018.08.01 |
[C언어] switch 이용한 계산기 (0) | 2018.08.01 |