<C프로그래밍-새내기를 위한 첫 C 언어 책>연습문제 chapter 6 (11, 12, 13)

11. 키보드로 10진 정수를 입력받은 후 입력받은 정수를 8진수, 10진수, 16진수의 형태로 출력하는 매크로 함수를 정의하시오.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#define NUMBER(x) printf(">>결과:8진수 10진수 16진수\n\t%o\t%d\t%x", x, x, x)
 
int main(void)
{
    int num;
    printf("정수 입력: ");
    scanf_s("%d"&num);
    NUMBER(num);
    
    return 0;
}
cs

 

 

12. 11번 문제에서 정희한 매크로를 이용해 1~15를 8진수, 10진수, 16진수로 출력하는 프로그램을 작성하시오.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#define NUMBER(x) printf("   %2o\t %2d\t%2x\n", x, x, x)
 
int main(void)
{
    printf("----------------------------\n");
    printf("  8진수\t10진수\t16진수\n");
    printf("----------------------------\n");
    for(int i = 1; i <= 15; i++)
        NUMBER(i);
    printf("----------------------------\n");
    return 0;
}
cs

 

 

13.난수를 생성하여 이 난수가 사용자에게서 입력받은 최솟값과 최댓값 사이의 범위에 포함되면 난수를 출력하고, 아니면 [Esc] 키를 누를 때까지 계속 난수를 생성하는프로그램을 작성하시오.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define ESC 0x1B
 
int main(void)
{
    srand((unsigned int)time(NULL));
    char ch = 0;
    int num, min, max;
 
    printf("최솟값: ");
    scanf_s("%d"&min);
    printf("최댓값: ");
    scanf_s("%d"&max);
    do
    {
        num = rand();
        if(min <= num && num <= max)
            printf("%d ", num);
        if (_kbhit())
            ch = _getch();
    } while (ch != ESC);
    return 0;
}
cs