반응형
풀이
더보기
import java.util.Scanner;
public class Quiz01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("수를 입력하세요 : ");
int number = scan.nextInt();
System.out.println(sum(number));
scan.close();
}
public static int sum(int a) {
int sum = 0;
for (int i = 0; i < a; i++) {
sum += i;
if (sum > 100) {
break;
}
}
return sum;
}
}
풀이
더보기
import java.util.Scanner;
public class Quiz01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("5개의 수를 입력하세요 : ");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();
int num4 = scan.nextInt();
int num5 = scan.nextInt();
min(num1, num2, num3, num4, num5);
scan.close();
}
public static void printMin(int num1,int num2,int num3,int num4,int num5) {
//System.out.println("최소값은 " + Math.min(num1, Math.min(num2, Math.min(num3, Math.min(num4, num5)))));
int min = num1;
if (min > num2) {
min = num2;
}
if (min > num3) {
min = num3;
}
if (min > num4) {
min = num4;
}
if (min > num5) {
min = num5;
}
System.out.println("최소값은 " + min);
}
}
풀이
더보기
import java.util.Scanner;
public class Quiz01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("수를 입력하세요 : ");
int number = scan.nextInt();
System.out.println(prime(number));
scan.close();
}
public static boolean prime(int a) {
for (int i = 2; i <= Math.sqrt(a); i ++) { // 루트를 씌운다
if (a % i == 0) {
return false;
}
}
return true;
}
반응형
'Algorithm > etc' 카테고리의 다른 글
[코딩연습] 2차원 배열 (0) | 2021.11.05 |
---|---|
[코딩연습] 배열 연습 (0) | 2021.11.04 |
[코딩연습] 메소드 생성 연습 1 (0) | 2021.11.02 |
[코딩연습] if문 ~ switch문 (0) | 2021.10.20 |
[코딩연습] switch 문 (0) | 2021.10.20 |