๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Welcome.
:: C_ ๐Ÿšฉ/์˜ˆ์ œ

[C์–ธ์–ด] ์˜ˆ์ œ ํ’€๊ธฐ if, if else, else -์˜์–ด ์„œ์ˆ˜ ํ‘œํ˜„ ์‹œ์Šคํ…œ-

by EunBird 2021. 2. 14.

๋ฌธ์ œ:

 

์˜์–ด๋กœ ์„œ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ธ๋‹ค.

1st  2nd  3rd  4th  5th  6th  ... 10th

11th 12th 13th 14th 15th  ...  20th

21st 22nd 23rd 24th 25th  ...  30th

31st 32nd 33rd 34th 35th  ...  40th

41st 42nd 43rd 44th 45th  ...  50th

...

91st 92nd 93rd 94th 95th  ...  99th

 

1~99 ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์˜์–ด ์„œ์ˆ˜ ํ‘œํ˜„์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 


#include<stdio.h>
void main() {


int a;  //์ •์ˆ˜๊ณต๊ฐ„a ์ƒ์„ฑ.
printf("์ˆซ์ž์ž…๋ ฅ:");
scanf("%d", &a);  //a์— ์ˆซ์ž์ €์žฅ.

int n = a % 10; //a๋ฅผ 10์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ n์— ์ €์žฅ.
if (a >= 11 && a <= 20) {  //์กฐ๊ฑด๋ฌธ: a๊ฐ€ 11~20 ์ธ๊ฐ€?
printf("%dth", a);
     }
else if (n == 1 && a != 11) {  //์กฐ๊ฑด๋ฌธ: n์ด 1์ด๊ณ  a๋Š” 11์ด ์•„๋‹Œ๊ฐ€?
printf("%dst", a);
     }
else if (n == 2 && a != 12) {  //์กฐ๊ฑด๋ฌธ: n์€ 2์ด๊ณ  a๋Š” 12๊ฐ€ ์•„๋‹Œ๊ฐ€?
printf("%dnd", a);
     }
else if (n == 3 && a != 13) {  //์กฐ๊ฑด๋ฌธ: n์€ 3์ด๊ณ  a๋Š” 13์ด ์•„๋‹Œ๊ฐ€?

printf("%drd", a);
     }
else  // ๊ทธ๋ฐ–์˜ ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ.
printf("%dth", a);
     }

 }

728x90

๋Œ“๊ธ€