본문 바로가기
프로그래밍 문제/C, C++ Problem

[C/C++] 기초플러스 5장 연습문제

by Physics 2022. 3. 4.
728x90

1. 분수로 주어지는 시간을 시간 수와 분 로 변환하는 프로그램을 작성하여라. 60을 나타내는 기호 상수를 만들기 위해 #define 또는 const를 사용하라. 사용자가 반복적으로 값을 입력할 수 있도록 while 루프를 사용하고 0또는 0보다 작은 값이 입력되면 루프를 끝낸다. 

#include <stdio.h>
#define Hour_to_Min 60

int main(void){

	float time;
	printf("#####################################\n");
	printf("Please, write down a time (unit: hour)\n");
	printf("Example: 1.25 hour\n");


	while(1){
		printf("If you want to quit this program, please write an integer equal or less than zero.\n");
		printf("Your time:");
		scanf("%f", &time);
		if(time <= 0) break;

		printf("your time is %d hour and %d minute\n",(int)time, (int)((time - (int)time) * Hour_to_Min));
	}
}

 

2. 하나의 정수를 요구하여, 그 값부터 시작하여 그보다 10만큼 큰 값까지의 모든 정수들을 출력하는 프로그램을 작성하라. (즉, 입력이 5이면 5에서 15까지 출력한다.) 각각의 출력값을 스페이스 또는 탭이나 개행으로 분리시켜라.

#include <stdio.h>

int main(void){
	int num;

	printf("Please write down an integer:");
	scanf("%d", &num);
	for (int i = num; i <= num + 10; i++){
		printf("%d\t", i);
	}
	printf("\n");
}

 

3. 사용자에게 날 수를 입력하도록 요구하여, 그 값을 주 수와 날 수로 변환하는 프로그램을 작성하여라. 예를들어 18일은 2주, 4일로 변환되어야 한다. 결과를 다음과 같은 포멧으로 출력하여라; "18일은 2주, 4일입니다."
사용자가 반복적으로 날짜를 입력할 수 있도록 while 루프를 사용하고, 사용자가 0이나 -20과 같은 양수가 아닌 값을 입력하면 루프를 끝낸다. 

#include <stdio.h>
int main(void){
	printf("Please write the number of days you want:\n");
	const int week =  7;
	int days;

	while(1){
		printf("If you want to quit this program, please put arbitrary integer less than zero.\n");
		scanf("%d",&days);
		if (days <= 0) break;
		printf("the number of %d days is %d week and %d days\n", days, days/week, days%week);
	}
	printf("Program end\n");
}

 

4. 사용자에게 키를 센티미터 단위로 입력하도록 요구하여 센티미터와 피트와 인치를 함께 출력하는 프로그램을 작성하여라. 센티미터와 인치는 소수부도 허용해야 한다. 프로그램은 양수가 아닌 값을 입력할 때까지 키를 계속 입력할 수 있도록 해야 한다. 

#include <stdio.h>

int main(void){
	float height, inch;
	int feet;
	const float cm_to_feet = 30.48;
	while(1){
		printf("Please write down your height in a unit of centimeter (if you want to quit, <=0): ");
		scanf("%f", &height);
		if(height<= 0)	break;
		feet = (int)(height/cm_to_feet);
		inch =  height - feet;
		printf("%.2f cm is %d feets and %.2f inches \n", height, feet, inch);
	}
	printf("Program is ended\n");
}

 

5. 사용자에게 하나의 float형 수를 입력하도록 요구하여, 그 수의 세제곱을 출력하는 프로그램을 작성하여라. 그 값의 세제곱을 구하여 출력하는 사용자 함수를 작성하여라. 

#include <stdio.h>

float cubic(float);
int main(void){
	printf("Please write down one number:");
	float num;
	scanf("%f", &num);
	printf("the cubic of your number %.3f is %.3f\n", num, cubic(num) );

}
float cubic(float a){ return a * a * a; };

 

6. 사용자에게 화씨온도를 입력하도록 요구하는 프로그램을 작성하여라. 이 프로그램은 화씨온도를 double형 수로 읽고, 그것을 Temperature()라는 사용자 함수에 전달인자로 전달해야 한다. 이 함수는 그 값에 상당하는 섭씨온도와 절대온도를 계산하여 출력하되, 세 가지 온도를 소수점 아래 두자리까지의 정밀도로 표시해야 한다. 이 프로그램은 각각의 값에 단위 표시를 하여 구별할 수 있도록 해야한다. 
 1) 섭씨온도 = 1.8 * 화씨온도 + 32.0 
 2) 절대온도 = 섭씨온도 + 273.16
Temperature()함수는 const를 사용하여 변환 공식에 나타나는 세 개의 상수를 기호 표현으로 나타내야 한다. main()함수는 사용자가 온도를 반복적으로 입력할 수 잇도록 루프를 사용해야 한다. 그 루프는 q또는 수치형이 아닌 값이 입력되면 종료된다. 

#include <stdio.h>

void Temperature(float f_temp){
	const float coeff1 = 1.8, coeff2 = 32.0, coeff3 = 273.16;
	float c_temp =  coeff1 * f_temp + coeff2;
	float abs_temp = c_temp + coeff3;
	
	printf("your temperature in the unit of fahrenheit: %.2f \n", f_temp);
	printf("your temperature in the unit of celsius: %.2f \n",c_temp);
	printf("your temperature in the unit of Kelvin: %.2f\n ", abs_temp);
}
int main(void){
	printf("Please write down a temperature in the unit of fahrenheit (if you want to quit, press q): ");
	float temp;
	while(scanf("%f", &temp)){
		Temperature(temp);
		printf("Please write down a temperature in the unit of fahrenheit (if you want to quit, press q): ");
	}	
	printf("Program is ended\n");

}
728x90

댓글