본문 바로가기

CS Study/C

2. whlie 문

1. 두 정수 사이의 모든 정수 합 구하기

#include <stdio.h>
int main(void) {
	int a;
	int b;
	int count;
	int add;
	int c;
	count = 0;
	add = 0;
	printf("Enter two integers : ");
	scanf_s("%d%d", &a, &b);
	c = a;
	while (count <= b - a) {
		add += c;
		c += 1;
		count += 1;

	}
	printf("The sum of all integers between %d and %d is %d", a, b, add);
}

2.  정수의 팩토리얼 계산 값 구하기

#include <stdio.h>
int main(void) {
	int a;
	int temp;
	int fact;
	fact = 1;
	printf("Enter a positive integer : ");
	scanf_s("%d",&a);
	temp = a;
	while (a < 0) {
		printf("Enter a positive integer : ");
		scanf_s("%d", &a);
		temp = a;
	}
	
	while (a > 0) {
		fact *= a;
		a -= 1;
	}
	
	printf("%d! is %d", temp, fact);
}

3. 정수안의 7의 개수 찾기

#include <stdio.h>
#include <math.h>
int main(void) {
	int num;
	int cipher;
	int ten;
	int a;
	int count;
	int temp;
	printf("Enter a number : ");
	scanf_s("%d", &num);
	cipher = 0;
	ten = 1;
	count = 0;
	temp = num;
	
	while (num/ten != 0) {
		cipher += 1;
		ten *= 10;
	}
	
	while (cipher >= 0) {
		a = num / pow(10, cipher);
		if (a == 7) {
			count ++;

			num -= a * pow(10, cipher);
			cipher -= 1;
		}
		else {
			num -= a * pow(10, cipher);
			cipher -= 1;
		}
		
		
	}
	printf("The number %d has %d seven(s) in it", temp, count);
	

}

4. 약수인지 아닌지 판단하기

#include <stdio.h>
int main(void) {
	int a;
	int b;
	printf("Input two integers : ");
	scanf_s("%d%d", &a, &b);
	if (a%b == 0) {
		printf("%d is a multiple of %d by a factor of %d",a,b,a/b);

	}
	else {
		printf("%d is not a multiple of %d ", a, b);
	}
}

5. 판매량에 따른 월급 출력

#include <stdio.h>
int main(void) {
	int a;
	int b;
	int c;
	int d;
	int a_1;
	int b_1;
	int c_1;
	int d_1;
	printf("Enter Employee 1's sales in dollars : ");
	scanf_s("%d", &a);
	if (a < 3000) {
		printf("Employee 1's salary is 200\n\n");
	}
	else {
		a_1 = a * 0.09 + 200;
		printf("Employee 1's salary is %d\n\n", a_1);

	}


	printf("Enter Employee 2's sales in dollars : ");
	scanf_s("%d", &b);
	if (b < 3000) {
		printf("Employee 2's salary is 200\n\n");
	}
	else {
		b_1 = b * 0.09 + 200;
		printf("Employee 2's salary is %d\n\n", b_1);

	}


	printf("Enter Employee 3's sales in dollars : ");
	scanf_s("%d", &c);
	if (c < 3000) {
		printf("Employee 3's salary is 200\n\n");
	}
	else {
		c_1 = c * 0.09 + 200;
		printf("Employee 3's salary is %d\n\n", c_1);

	}


	printf("Enter Employee 4's sales in dollars : ");
	scanf_s("%d", &d);
	if (d < 3000) {
		printf("Employee 4's salary is 200\n\n");
	}
	else {
		d_1 = d * 0.09 + 200;
		printf("Employee 4's salary is %d\n\n", d_1);

	}

}

'CS Study > C' 카테고리의 다른 글

6. Array / MultiArray / String  (0) 2022.04.06
5. function - recursive  (0) 2022.04.06
4. math.h / time.h  (0) 2022.04.06
3. do-while / switch / for  (0) 2022.04.06
1. basic / scanf / if  (0) 2022.04.06