본문 바로가기
C/☞

[20] 연습문제 : while

by TR. 2020. 7. 14.

연습문제

 

# include <Windows.h>
# include <stdio.h>

void main(){
	// 문제1) 10~15출력
	int n = 10;
	// 문제2) 5~ -5 출력 (거꾸로 출력)	
	// 문제3) 1~10중에서 짝수만출력 (if 를 사용)
	// 문제4) 1~5 까지 전체합 1 + 2 + 3 + 4 + 5
	// 문제5) 1~5중에서 홀수의 합 출력 1 + 3 + 5
	// 문제6) 1~5중에서 홀수의 갯수 출력 ==> 3	


	system("pause");
}
더보기
# include <Windows.h>
# include <stdio.h>

void main(){
	
	// 문제1) 10~15출력
	int n = 10;
	while(n < 16){
		printf("문제1) %d" , n);printf("\n");
		n = n + 1;
	}

	// 문제2) 5~ -5 출력 (거꾸로 출력)
	n = 5;
	while(n > -6){
		printf("문제2) %d" , n);printf("\n");
		n = n - 1;
	}

	// 문제3) 1~10중에서 짝수만출력 (if 를 사용)
	n = 1;
	while(n < 11){
		if(n % 2 == 0){
			printf("문제3) %d" , n);printf("\n");
		}
		n = n + 1;
	}

	// 문제4) 1~5 까지 전체합 1 + 2 + 3 + 4 + 5
	int n = 1;
	int total = 0;
	// 1~5 까지 전체합 1 + 2 + 3 + 4 + 5
	while(n < 6){
		total = total + n;
		n = n + 1;
	}
	printf("합 : %d" , total); printf("\n");

	// 문제5) 1~5중에서 홀수의 합 출력 1 + 3 + 5
	total = 0;
	while(n < 6){
		if(n % 2 == 1){
			total = total + n;
		}
		n = n + 1;
	}
	printf("홀수의 합 : %d" , total); printf("\n");

	
	// 문제6) 1~5중에서 홀수의 갯수 출력 ==> 3		n = 1;
	total = 0;
	while(n < 6){
		if(n % 2 == 1){
			total = total + 1;
		}
		n = n + 1;
	}
	printf("홀수의 갯수 : %d" , total); printf("\n");


	system("pause");
}

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

[22] 난수 생성 (Random)  (0) 2020.07.14
[21] 반복문 : for  (0) 2020.07.14
[19] 반복문 : while  (0) 2020.07.14
[18] 반복문  (0) 2020.07.14
[17]-A. 로그인 처리  (0) 2020.07.14

댓글