SW 9일차

Guryena·2023년 1월 5일
0

CS

목록 보기
1/34

1.반복문에서 while 문과 do while 문의 차이는?

  • while : 조건을 주어지고 Logic 시행
	while(조건문){
    Logic
    }
  • do~while : Logic을 작성 후 조건을 후행
	do{
    Logic
    }while(조건문);

2.for 문에서 for 문이 실행되는 순서를 설명하시오.

for (int i = 0; i < 5; i++) { // int 선언 후 조건문 시행
			System.out.println("hello world " + i); // Logic시행
	//i++시행	} //반복문 종료

3. 9단을 출력하는 프로그램을 만드시오.(while 문 사용할것)

		int mul = 0; //출력변수
		int x = 9; //n단 변수
		int y = 1;
		while (y <= 10) {
			mul += x;
			System.out.println(x + " * " + y + " = " + mul);
			y++;

4.for 문을 활용하여, 1부터 100까지의 합을 구하시오.

//합계
		int n = 100;
		int sum = 0;
		for (int i = 0; i <= n; i++) {
			sum += i;

5.1부터 100까지의 홀수들의 합을 구하시오.

		int n = 100;
		int odd_num = 0;
		int odd_num_sum = 0;
		for (int i = 1; i <= n; i++) {
			odd_num = i;
			if (odd_num == 1 || odd_num % 2 == 1) {
				odd_num_sum += odd_num;
			}
		}
		System.out.println(odd_num_sum);
	}

6.반복문에서의 break 와 continue 를 설명하시오.

  • break; : 해당 Logic(예:반복문)을 종료
  • continue; : 해당 Logic(예:반복문)의 다음 연산을 시행

7.알파벳에서 10번째 문자를 출력하는 프로그램을 짜시오.

		int x = 65;
		int n = 9;
		int al = 26;
		for (int i = x; i < al + 65; i++) {
			if (i == x + n) {
				System.out.println((char) i);
			}
		}

8.아래를 프로그래밍 하시오.

  • 1과 1000 사이의 숫자중 3의 배수 이자 5의 배수인 첫번재 수는?
    [최소공배수]
		int x = 1000;
		int least_common_multiple = 0;
		boolean search = false;
		for (int i = 1; i <= x; i++) {
			if (i % 2 == 0 && i % 3 == 0) {
				search = true;
				least_common_multiple = i;
				break;
			}
		}
		if (search) {
			System.out.println(least_common_multiple);
		} else {
			System.out.println("not detected");
		}
		
  

9. 1과 1000 사이의 숫자중 2의 배수 이자 3의 배수인 수는 모두 몇개인가?

10.13.화폐매수 구하기

=126500 의 금액을 한국화폐으로 바꾸었을 때 각각 몇 개의 화폐가 필요한지 계산해서 출력하라.

예) int 126500;
오만원 : 2장
만원: 2장
오천원짜리 :1장
천원짜리: 1
오백원짜리 1개
백원짜리 0개

		int money = 126500;
		final int _50000won = 50000;
		final int _10000won = 10000;
		final int _5000won = 5000;
		final int _1000won = 1000;
		final int _500won = 500;
		final int _100won = 100;
		int balance = money;
	int m50000 = 0; //2장
	int m10000 = 0; //2장
	int m5000 = 0; //1장
	int m1000 = 0; //1장
	int m500 = 0; //1장
	int m100 = 0; //0장
	for (int i = 1; i <= 6; i++) {

		if (balance / _50000won > 0) {
			m50000 = balance / _50000won;
			balance = (balance % _50000won);
			System.out.println(m50000);
			System.out.println(balance);
			continue;
		} else if (balance / _10000won > 0) {
			m10000 = balance / _10000won;
			balance = balance % _10000won;
			System.out.println(m10000);
			System.out.println(balance);
			continue;
		} else if (balance / _5000won > 0) {
			m5000 = balance / _5000won;
			balance = balance % _5000won;
			System.out.println(m5000);
			System.out.println(balance);
			continue;
		} else if (balance / _1000won > 0) {
			m1000 = balance / _1000won;
			balance = balance % _1000won;
			System.out.println(m1000);
			System.out.println(balance);
			continue;
		} else if (balance / _500won > 0) {
			m500 = balance / _500won;
			balance = balance % _500won;
			System.out.println(m500);
			System.out.println(balance);
			continue;
		} else if (balance / _100won > 0) {
			m100 = balance / _100won;
			balance = balance % _100won;
			System.out.println(m100);
			System.out.println(balance);
			continue;
		} else {
			continue;
		}


	}

0개의 댓글