CS Study/C

4. math.h / time.h

Ryannn 2022. 4. 6. 13:39

1. 제곱수 출력

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a, b, c;
	int i;
	for (i = 0; i <= 2; i++)
	
		printf("Input two value : ");
		scanf_s("%1f%1f", &a, &b);
	
		if (a >= 0 && b >= 0) {
			c = sqrt(pow(a, 2) + pow(b, 2));
			printf("%1f", c);
		
		}
		if(a<0||b<0) {
			printf("too small!");
		}
	
}

2. 완전수인지 아닌지 판단하기

#include <stdio.h>
int main(void) {

	int i, n;
	int s=0;
	printf("Enter a numper between 0 and 1000 : ");
	scanf_s("%d", &n);

	for (i = 1; i < n; i++)
		if (n%i == 0)
			s += i;
	if (n == s) {
		printf("%d is perfect!", n);
	}
	else {
		printf("%d is not perfect...", n);
	}

	return 0;
}

3. 대문자 / 소문자 변환

#include <stdio.h>

int ulcase(char x);

int main(void) {

	char a;

	printf("Enter an alphabet : ");
	scanf_s("%c", &a);


	if (ulcase(a) == 1) printf("%c: Uppercase.\n", a);
	if (ulcase(a) == 0) printf("%c: Lowercase.\n", a);
	if (ulcase(a) == 2) printf("It must be an alphabet!");

	return 0;

}
int ulcase(char x) {
	if (x >= 65 && x <= 90) return 1;
	else if (x >= 97 && x <= 122) {
		return 0;
	}
	else {
		return 2;
	}
}

4. 코인 뒤집기 - 윗면, 아랫면 개수 출력

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int coin_toss(void);
int main(void)
{
	int toss;
	int heads = 0;
	int tails = 0;
	srand((unsigned)time(NULL));

	for (toss = 1; toss <= 100; toss++) {
		if (coin_toss() == 1) {
			heads++;
			printf("head ");
			if (toss % 10 == 0) {
				printf("\n");
			}
		}
		else {
			tails++;
			printf("tail ");
			if (toss % 10 == 0) {
				printf("\n");
			}
		}

	}
	printf("������ �ո�%d��\n", heads);
	printf("������ �޸�%d��\n", tails);
	return 0;
}

int coin_toss(void)
{
	int i = rand() % 2;
	if (i == 0)
		return 0;
	else
		return 1;
}