본문 바로가기
Programming/컴퓨터프로그래밍및실습

[컴프실] 5일차 실습문제

by leziwn.cs 2023. 7. 4.

//Pi를 기호상수로 만들고, 값을 입력받아 결과를 출력하시오.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	const double Pi = 3.141592;
	double r;
	double h;

	printf("r을 입력: ");
	scanf("%lf", &r);

	printf("h를 입력: ");
	scanf("%lf", &h);

	double v1;
	double v2;

	v1 = 1.0 / 2.0 * Pi * r * r * h;
	v2 = 4.0 / 3.0 * Pi * r * r * r;

	printf("v1 = %f\n", v1);
	printf("v2 = %f\n", v2);
	/*The divisions 1 / 2 and 4 / 3 are using integer division,
	which results in truncating the fractional parts. As a result, the calculated values for v1 and v2 will be incorrect.*/

	return 0;
}

 

//10진수를 입력 받아 8진수와 16진수로 출력하시오.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int x;

	printf("10진수 입력: ");
	scanf("%d", &x);
	printf("10진수 " "%d" "의 8진수는 000000%o\n", x, x); //x를 두 번씩 써야 함!!!
	printf("10진수 " "%d" "의 16진수는 000000x%X\n", x, x);

	/* 
	printf("10진수 " "%d" "의 8진수는 %o\n", x, x);
	printf("10진수 " "%d" "의 16진수는 %x\n", x, x);
	*/

	return 0;

}
  • %d: 10진수
  • %o: 8진수
  • %x: 16진수

 

/*두 개의 문자를 입력받아 ASCII 코드 값을 출력하고,
두 문자 코드 값을 더한 값의 문자를 출력하시오.*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
	char code1;
	char code2;

	printf("두 개의 문자 입력: ");
	scanf("%c %c", &code1, &code2);

	printf("첫번째 문자 코드값: %d\n", code1, code1);
	printf("두번째 문자 코드값: %d\n", code2, code2);
	printf("'%c'+'%c'의 문자는 %c\n", code1, code2, code1+code2);

	return 0;
}