AR삽질러

C로 시작하는 컴퓨터 프로그래밍4판 - 13장 실전예제(면적구하프로그램) 본문

C

C로 시작하는 컴퓨터 프로그래밍4판 - 13장 실전예제(면적구하프로그램)

아랑팡팡 2023. 6. 22. 08:19
728x90

면적 구하기 프로그램

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<math.h>
#include<stdlib.h>

int main(void) {

	int i = 0, count = 0, nrepeat;
	double x, y;
	printf("몇 회 반복할까요? : ");
	scanf("%d", &nrepeat);
	for (i = 0; i < nrepeat; i++) {
		x = (double)rand() / 32767; 
		y = (double)rand() / 32767;
		if ((x * x + y * y) <= 1) count = count + 1;
	}

	printf("%d회 중 %d회가 1/4원 안에 있으므로\n", nrepeat, count);
	printf("반지름이 1인 1/4원의 면적은 %4.3f임\n", (double)count / nrepeat);

	return 0;
}

728x90
반응형
LIST