JAVA

반복문(for문) 예제

순두부 호랑이 2022. 9. 4. 14:32
728x90
SMALL

Q1. 1부터 5까지 출력해주는 반복문을 작성하시오.

package review;

public class ex03 {

	public static void main(String[] args) {
		for(int i =0;i<=5; i++) {
			System.out.println(i);
		}
		
	}

}

Q2. "hello"를 10번 출력해주는 반복문을 작성하시오.

package review;

public class ex03 {

	public static void main(String[] args) {
		for(int i =0;i<=10; i++) {
			System.out.println("hello");
		}
		
	}

}

Q3. for문을 사용하여 21에서 57까지 출력하시오.

       for문을 사용하여 96에서 53까지 출력하시오.

       for문을 사용하여 21에서 57까지 수 중 홀수만 출력하시오.

package review;

public class ex03 {

	public static void main(String[] args) {
		
		// for문을 사용하여 21에서 57까지 출력하시오.
		for (int i = 21; i <= 57; i++) {
			System.out.print(i + " ");
		}
		System.out.println();
		
		// for문을 사용하여 96에서 53까지 출력하시오.
		for (int i = 96; i >= 53; i--) {
			System.out.print(i + " ");
		}
		System.out.println();

		// for문을 사용하여 21에서 57까지 수 중 홀수만 출력하시오.
		for (int i = 21; i <= 57; i+=2) {
			System.out.print(i + " ");
		}

	}

}

Q4. 정수1개를 입력받아 1부터 입력받은 정수까지 차례대로 출력해주는 프로그램을 작성하시오.

package review;

import java.util.Scanner;

public class ex03 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int num= sc.nextInt();
		
		for(int i =0;i<=num;i++) {
			System.out.print(i + " ");
		}
		

	}

}

Q5. 100 이하 두개의 정수를 입력 받아 작은 수부터 큰 수까지 차례대로 출력해주는 프로그램을 작성하시오.

package review;

import java.util.Random;
import java.util.Scanner;

public class ex02 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		
		if(num1<num2) {
			for(int i=num1; i<=num2; i++) {
				System.out.println(i);
			}
		}else {
			for(int i=num2; i<=num1; i++) {
				System.out.println(i);
			}
		}

		
		
		}
	}

Q6. 정수 2개를 입력받아 두 수 사이의 총 합을 출력하는 프로그램을 작성하시오.

package review;

import java.util.Scanner;

public class ex02 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		int sum=0;
		
		if(num1<num2) {
			for(int i=num1; i<=num2; i++) {
				sum+=i;
			}
		}else {
			for(int i=num2; i<=num1; i++) {
				sum+=i;
			}
		}
		System.out.println(sum);
		
		}
	}

Q7. 1~50까지 369게임을 출력하시오. 끝자리수가 3,6,9일때는 "박수"를 출력 끝자리수가 5일 때는 "으악"을 출력 그 외의 수는 숫자를 그대로 출력하시오.

package review;
public class ex02 {

	public static void main(String[] args) {
		
		for(int i=1 ; i<=50; i++) {
			if(i%10==5) {
				System.out.println("으악");
			}else if(i%10==3||i%10==6||i%10==9) {
				System.out.println("박수");
			}else {
				System.out.println(i);
			}
		}
		
		}
	}

Q8. 두 개의 정수를 입력받고 두 정수 모두 0을 입력받았을 때 입력을 중단하는 프로그램을 작성하시오.

package review;

import java.util.Scanner;

public class ex02 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
				
		while(true) {
			int num1 = sc.nextInt();
			int num2 = sc.nextInt();
			
			if(num1==0 && num2==0) {
			break;	
			}

		}
		
		}
}

Q9. 1-2+3+-4+...+99-100을 다음과 같이 출력한 후 계산 결과를 출력하시오.

package review;

public class ex02 {

	public static void main(String[] args) {
		
		int sum=0;
		
		for(int i =1; i<=100; i++) {
			if(i%2==0) {
				System.out.print(-i+" ");		
				sum-=i;
			}else {
				System.out.print(i);
				sum+=i;
			}
		}
		System.out.println();
		System.out.println("결과: "+ sum);
		}
}

Q10. (77*1)+(76*2)+(75*3)+...+(1*77)을 계산하여 결과를 출력하시오.

package review;

public class ex03 {

	public static void main(String[] args) {
		
		int down=77;
		int up =1;
		int sum=0;
		
		for(int i=1;i<=77;i++) {
			sum+=(up*down);
			up++;
			down--;
		}
		System.out.println(sum);

		
	}
}

Q11. for문을 사용하여 A~Z까지 출력하시오

package review;

public class ex03 {

	public static void main(String[] args) {
		
		for(char i ='A'; i<='Z'; i++) {
			System.out.print(i+" ");
		}

		
	}
}

Q12. for문을 사용하여 구구단 2단을 출력하시오

package review;

public class ex03 {

	public static void main(String[] args) {
		
		for(int i=1; i<=9; i++) {
			System.out.printf("2 * %d = %d\n",i,2*i);
		}

		
	}
}

Q13. 원하는 단과 범위를 입력하여 다음과 같이 출력하시오.

package review;

import java.util.Scanner;

public class ex03 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("단 입력 : ");
		int dan = sc.nextInt();
		System.out.print("범위 입력: ");
		int num = sc.nextInt();
		
		for(int i=1; i<=num; i++) {
			System.out.printf("%d * %d = %d\n",dan,i,dan*i);
		}

		
	}
}

Q14. 구구단 2단~9단까지 출력하시오.

package review;

public class ex04 {

	public static void main(String[] args) {
		
		for(int i=2; i<=9; i++) {
			System.out.println("=="+i+"단==");
			for(int j=1; j<=9; j++) {
				System.out.printf("%d * %d = %d\n",i,j,i*j);
			}
		}
		
	}

}

Q15. 구구단 2단~9단까지 출력하시오.

package review;

public class ex04 {

	public static void main(String[] args) {
		
		for(int i=2; i<=9; i++) {
			System.out.print(i+"단 : \t");
			for(int j=1; j<=9; j++) {
				System.out.printf("%d * %d = %d\t",i,j,i*j);
			}
			System.out.println();
		}
		
	}

}

Q16. 구구단 2단~9단까지 출력하시오.

package review;

public class ex04 {

	public static void main(String[] args) {
		
		for(int i=1; i<=9; i++) {
			for(int j=2; j<=9; j++) {
				System.out.printf("%d * %d = %d\t",j,i,i*j);
			}
			System.out.println();
		}
		
	}

}

Q17.다음과 같이 출력하시오.(별찍기)

package review;

public class ex04 {

	public static void main(String[] args) {
		
		for(int i=1; i<=5 ; i++) {
			for(int j=1; j<=i; j++) {
				System.out.print("*");
			}
			System.out.println("");
		}
		
	}
}

 

Q18.다음과 같이 출력하시오.(별찍기)

package review;

public class ex04 {

	public static void main(String[] args) {
		
		for(int i=5; i>=1 ; i--) {
			for(int j=1; j<=i; j++) {
				System.out.print("*");
			}
			System.out.println("");
		}
		
	}
}

Q18.다음과 같이 출력하시오.(별찍기)

728x90
LIST