[C언어] if~else와 switch 비교



IF)

#include<stdio.h>

int main(){

int a =1;

{if(a==1){

printf("1번입니다\n");

}

else if(a==2){

printf("2번입니다\n");

}

else if(a==3){

printf("3번입니다\n");

}
else{
printf("다시 입력하세요\n");
}

}

return 0;

}




SWITCH)


#include<stdio.h>

int main(){

int a =1;

switch(a){

case 1 : printf("1번입니다\n");

break;

case 2 : printf("2번입니다\n");

break;

case 3 : printf("3번입니다\n");

break;

default : printf("다시 입력하세요\n");

}

return 0;

}


if statement 와 switch statement를 비교해보았는데, 어떤 것이 쉽다, 간결하다, 보기 좋다고 표현하는 책들이 여럿 있습니다.

그리고 대부분의 책에서는 if statement를 배우고 나서 switch statement를 다루는데 이유로 드는 것이 switch가 더 쉽다, 간결하다고 표현합니다.

하지만 판단은 본인의 몫이고 프로그래밍이 누구에게 좋아 보이는 것이 일순위가 아니기 때문에 본인이 필요하다고 느끼는 것에 더 집중하면 될 듯 싶습니다. 이상 comparison between if statement and switch statement 였습니다.



[C언어] 최대공약수, 최소공배수 구하기


최대공약수 : Greatest Common Divisor

최소공배수 : Least Common Multiple


#include<stdio.h>

int main(void){

int GCD, LCM, a, b, num1, num2, r;

printf("enter two numbers\n");

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

a=num1;

b=num2;

while(b!=0){

r=a&b;

a=b;

b=r;

}

GCD=a;

LCM=(num1*num2)/GCD;

printf("greatest common divisor of %d and %d = %d", num1, num2, GCD);

printf("least common multiple of %d and %d = %d", num1, num2, LCM);

getch();

return 0;

}

[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;

}


[C언어] switch 이용한 계산기


1)아래는 캐릭터 한 글자를 이용한 switch 계산기입니다.

Below is the calculator using switch with one byte character.


#include <stdio.h>

Int main(void){

int num1, num2=0;

char a=0;


printf("안녕하세요. switch 계산기 입니다.\n");

printf("연산자 +, - , *, /  중 하나를 입력해주세요.");

scanf("%c", &a);


switch(a){

case '+' : printf("+를 선택하셨습니다. 더할 두 숫자를 입력해주세요 :");

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

printf("더한 합은 : num1 + num2 = ", num1+num2);

break;

case '-' :  printf("-를 선택하셨습니다. 뺄셀할 두 숫자를 입력해주세요 :");

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

printf("뺄셈 값은 : num1 - num2 = ", num1-num2);

break;

case '*' :  printf("*를 선택하셨습니다. 곱할 두 숫자를 입력해주세요 :");

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

printf("곱한 값은 : num1 * num2 = ", num1*num2);

break;

case '/' :  printf("/를 선택하셨습니다. 더할 두 숫자를 입력해주세요 :");

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

printf("나눈 값은 : num1 / num2 = ", num1/num2);

break;

default : printf("잘못된 연산자를 입력했습니다. 다시 입력하세요.\n");

}

return 0;

}



2)아래는 정수를 이용한 switch 계산기입니다.

Below is the calculator using switch with one integer variable.


#include <stdio.h>

Int main(void){

int num1, num2=0;

int variable=0;


printf("안녕하세요. switch 계산기 입니다.\n");

printf("연산자 1: +(덧셈), 2: -(뺄셈) , 3: *(곱셈), 4: /(나눗셈)  중 하나를 입력해주세요.");

scanf("%d", &variable);


switch(variable){

case 1 : printf("+를 선택하셨습니다. 더할 두 숫자를 입력해주세요 :");

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

printf("더한 합은 : num1 + num2 = ", num1+num2);

break;

case 2 :  printf("-를 선택하셨습니다. 뺄셀할 두 숫자를 입력해주세요 :");

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

printf("뺄셈 값은 : num1 - num2 = ", num1-num2);

break;

case 3 :  printf("*를 선택하셨습니다. 곱할 두 숫자를 입력해주세요 :");

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

printf("곱한 값은 : num1 * num2 = ", num1*num2);

break;

case 4 :  printf("/를 선택하셨습니다. 더할 두 숫자를 입력해주세요 :");

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

printf("나눈 값은 : num1 / num2 = ", num1/num2);

break;

default : printf("잘못된 연산자를 입력했습니다. 다시 입력하세요.\n");

}

return 0;

}

+ Recent posts