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

[C 기초 플러스] 3장 연습문제

by Physics 2022. 2. 25.
728x90

문제 2

66과 같은 ASCII 코드값을 사용자에게 입력하도록 요청하고 그 ASCII 코드값에 해당되는 문자를 출력하는 프로그램을 작성하여라 

#include <stdio.h>
#include <stdlib.h>

int main(void){
	printf("################################################################################\n");
	printf("This program will translate a number you give into a corresponding ASCII code\n");
	printf("################################################################################\n");
	
	int num; 
	while (1){
		printf("Please give a number in a range from 0 to 127\n");
		printf("Number: ");
		scanf("%i", &num);
		if(num >=0 && num <= 127) break;
	}
	printf("The ASCII code corresponding to %i: %c\n", num, num);

}

 

문제 3 

경보음을 한 번 울리고, 다음과 같은 텍스트를 화면에 출력하는 프로그램을 작성하여라.

#include <stdio.h>
#include <stdlib.h>

int main(void){
	printf("\a\n");
	printf("Sally who is surprised with a sudden noise is yelling, \"Pumking King!\"\n");
}

 

문제 4 

부동소수점 하나를 읽고, 처음에는 소수점 표기로, 다음에는 지수표기로 출력하는 프로그램을 작성하여라. 

#include <stdio.h>
#include <stdlib.h>

int main(void){
	printf("#################################################\n");
	printf("Write down a real number with decimal point\n");
	printf("#################################################\n");
	
	float num;
	scanf("%f", &num);

	printf("The number which you write is %f or %e.\n", num, num);


}

 

문제 5

1년은 약 3.156x10^(7)초에 해당한다. 나이를 햇수로 입력 받아, 초 단위로 출력하는 프로그램을 작성하여라. 

#include <stdio.h>
#include <stdlib.h>

int main(void){
	
	float age;
	const float year_to_sec = 3.157e7;
	printf("Write down your age\n");
	scanf("%f", &age);
	printf("In second uni, your age is %.1f second\n", age * year_to_sec);
}

 

문제 6

물 분자의 질량은 3.0x10^(-23)그램이다. 물 1쿼트는 약 950그램이다. 쿼트 단위로 물의 양을 입력 받아, 그 안에 들어 있는 물 분자의 갯수를 출력하는 프로그램을 작성하여라.

#include <stdio.h>
#include <stdlib.h>

int main(void){
	const double w_mass = 3.0e-23;
	const double quart_to_mass = 950;
	double num;

	printf("Write down the amount of water (unit: quart)\n");
	scanf("%lf", &num);
	printf("the number of water molecules in %lf quart is %e\n", num, num * quart_to_mass / w_mass);
}

 

728x90

댓글