1. JAVA/1.2 실습
JAVA_05_실습_반복문과 조건문3
마느링
2022. 3. 16. 20:44
문제 1]
3 ~ 100 사이의 정수를 입력받아서
이 정수가 소수인지 판단하는 프로그램을 작성하세요
참고 : 소수 ==> 1과 자신으로만 나누어지는 수
보너스 ] 3 ~ 100 사이의 숫자 중에서 소수만 출력하시오
public static void main(String[] args) {
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.print("3 ~ 100까지의 정수를 입력하시오");
int input = sc.nextInt();
String str = "";
for(int i = 1; i<=input; i++) {
if(input%i==0) {
count++;
System.out.println(count+" "+i);
if(count<3) {
str = "소수입니다.";
}
}
else {
str="소수가 아닙니다.";
}
}
System.out.println(str);
count = 0;
for(int i= 3; i<=100;i++) {
for(int j = 1;j<=i;j++) {
if(i%j==0) {
count++;
System.out.println(i+" "+j);
}
}
}
}
public class Ex03 {
public static void main(String[] args) {
/*
1 2 3 4 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
*/
for(int i = 0; i<2;i++) {
for(int j = 1;j<6;j++) {
System.out.print(j+" ");
}
System.out.println();
}
for(int i = 1; i<5;i++) {
for(int k = 1;k<6;k++) {
System.out.print((k+i)+" ");
}
System.out.println();
}
System.out.println("======================");
/*
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
*/
for(int i = 1; i<25;i+=5) {
for(int j = 0; j<5;j++) {
System.out.print(i+j +" ");
}
System.out.println();
}
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/
System.out.println("======================");
for(int i = 5; i>0;i--) {
for(int j = 0; j<=5-i;j++) {
System.out.print(j+1+" ");
}
System.out.print(" ");
System.out.println();
}
/*
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/
System.out.println("======================");
int sum = 1;
for(int i = 0; i<5;i++) {
sum+=i;
System.out.print(sum+" ");
for(int j = 1; j<=i;j++) {
System.out.print((sum+j)+" ");
}
System.out.println();
}
}
}