1:1 2:3 3:6 4:10 5:15 6:21 … 100이전의 총합들을 다음과 같이 출력되면 된다.
힌트 : 조건식에 복잡한 수식이 들어 갈수 도 있다.
int sum = 0;
for(int i = 1;i < 101;i++){
sum += i;
System.out.print(i+":" +sum + " ");
}
java.util.Scanner sc = new java.util.Scanner(System.in);
String str = sc.nextLine();
int repeat = Integer.parseInt(sc.nextLine());
for(int i = 0;i < repeat;i++){
System.out.println(str);
}
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
int sum = 0;
for(int i = a;i <= b;i++){
sum += i;
}
System.out.println(sum);
5 6를 입력 받으면 5부터 6개 5 6 7 8 9 10 이 화면에 출력 된다.
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
for(int i = a;i < (a + b);i++){
System.out.print(i + " ");
}
java.util.Scanner sc = new java.util.Scanner(System.in);
String input = "";
while(true){
intput = sc.nextLine();
if(input.equals("종료")){
break;
}
}
힌트) 0으로 나눌수 없듯이 0의 나머지는 구할수 없다.
ex) 어떤 수에 대해서 나누어 떨어지는 수를 약수라고 한다.
100을 2로 나눈 나머지는 0이 된다.
100은 2로 나누어 떨어 지므로, 2는 100의 약수이다.
3은 100으로 나눠 떨어지지 않으므로 약수가 아니다.
100의 모든 약수를 구하려면 100보다 크면 더 이상 나눌 수 없으므로 1~100사이의 수들로 하나씩 100을 나눠서 나누어 떨어 지는지 판별하여 나누어 떨어지는 수들이 100의 약수가 된다.
1,2,4,5,..100 이 100의 약수에 해당한다.
int target = 100;
for(int i = 1;i < target;i++){
if(target % i == 0){
System.out.print(i + " ");
}
}
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
java.util.ArrayList<Integer> aFactors = new java.util.ArrayList<Integer>();
java.util.ArrayList<Integer> bFactors = new java.util.ArrayList<Integer>();
java.util.ArrayList<Integer> commonFactors = new java.util.ArrayList<Integer>();
for(int i = 1;i <= a;i++){
if(a % i == 0){
aFactors.add(i);
}
}
for(int i = 1;i <= b;i++){
if(b % i == 0){
bFactors.add(i);
}
}
for(int i = 0;i < aFactors.toArray().length;i++){
for(int j = 0;j < bFactors.toArray().length;j++){
if(aFactors.toArray()[i] == bFactors.toArray()[j]){
commonFactors.add(aFactors.toArray()[i]);
}
}
}
for(int i = 0;i < commonFactors.toArray().length;i++){
System.out.println(commonFactors.toArray()[i] + " ");
}
0부터 하나씩 증가시키며 두수 모두 나눠지는 수를 출력하면 된다.
출력된 수중에서 가장 큰수를 입력받은 두수의 최대공약수라고 한다.
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
java.util.ArrayList<Integer> aFactors = new java.util.ArrayList<Integer>();
java.util.ArrayList<Integer> bFactors = new java.util.ArrayList<Integer>();
int commonFactor = 0;
for(int i = 1;i <= a;i++){
if(a % i == 0){
aFactors.add(i);
}
}
for(int i = 1;i <= b;i++){
if(b % i == 0){
bFactors.add(i);
}
}
for(int i = 0;i < aFactors.toArray().length;i++){
for(int j = 0;j < bFactors.toArray().length;j++){
if(aFactors.toArray()[i] == bFactors.toArray()[j]){
commonFactor = aFactors.toArray()[i];
}
}
}
System.out.println(commonFactor);
java.util.Scanner sc = new java.util.Scanner(System.in);
int sum = 0;
int a = 0;
while(sum <= 100){
a = Integer.parseInt(sc.nextLine());
sum += a;
}
System.out.println(sum);
입력이 끝나면 잘못 입력한 횟수와 제대로 입력한 횟수를 출력하고
사용자가 제대로 입력한 총합을 출력하는 프로그램을 만들어 보자.
java.util.Scanner sc = new java.util.Scanner(System.in);
int fail = 0;
int success = 0;
int input = 0;
int sum = 0;
while(success != 3){
try {
input = Integer.parseInt(sc.nextLine());
} catch(Exception e){
fail++;
continue;
}
if(input > 0 && input < 11){
sum += input;
success++;
} else {
fail++;
}
}
ex) 어떤수에 0,1,2,3,4.. 를 곱해서 나온 수를 어떤수의 배수라 한다.
int target = 4;
for(int i = 0;i < 10;i++){
System.out.println((target * i) + ",");
}
ex)2의 배수도 되고 3의 배수도 되는 공배수는 6,12,18,24 등이 있다.
이중에서 가장 작은 수 6를 최소 공배수 라고 한다.
2에 어떤수를 곱해서 나온 결과가 2의 배수 이므로 어떤 수를 2로 나누어 나머지가 0이 되면 2의 배수이다.
1 부터 하나씩 증가시켜 입력 받은 두 수가 모두 나누어 떨어지는 수들은 공배수 이고 이중 가장 먼저 찾은 수가 가장 작은 수이므로 처음 찾은수가 최소 공배수이다.
2와 3의 최소 공배수는 6이다.
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
int leastCommonMultiple = 0;
if(a > b){
if(a % b == 0){
leastCommonMultiple = a;
} else {
leastCommonMultiple = a * b;
}
} else {
if(b % a == 0){
leastCommonMultiple = b;
} else {
leastCommonMultiple = a * b;
}
}
System.out.println(leastCommonMultiple);
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
int c = Integer.parseInt(sc.nextLine());
int tmp;
if(a > b) {
tmp = a;
a = b;
b = tmp;
}
if(a > c) {
tmp = a;
a = c;
c = tmp;
}
if(b > c) {
tmp = b;
b = c;
c = tmp;
}
System.out.println(a);
System.out.println(c);
int success = 0;
int under60 = 0;
int input = 0;
while(success != 5){
input = Integer.parseInt(sc.nextLine());
if(input <= 100){
success++;
}
if(input <= 60){
under60++;
}
}
System.out.println(under60);
2,3,5같은 경우 1과 본인 자신만으로 나눠지므로 소수이다.
수를 하나 입력받아 소수인지 아닌지 판별하는 프로그램을 구현하시오.
ex) 2부터 본인 보다 하나 작은 숫자를 차례대로 나눠서 나누어 떨어지는 수가 없으며 1과 자기 자신만 나눠지는 수이므로 소수에 해당한다.
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
java.util.ArrayList<Integer> aFactors = new java.util.ArrayList<Integer>();
for(int j = 1;j <= a;j++){
if(a % j == 0){
aFactors.add(j);
}
}
System.out.println("소수" + (aFactors.size() == 2 ? "입니다." : "가 아닙니다."));
java.util.Scanner sc = new java.util.Scanner(System.in);
int input = Integer.parseInt(sc.nextLine());
int count = 0;
for(int i = 1;i <= 1000;i++){
if(input == 0){
System.out.println("0을 입력했습니다. 더 이상 진행 하지 않습니다.");
break;
}
if(i % input == 0 && input != 0){
count++;
}
}
System.out.println(count);
ex) 3을 입력하면 한줄에 3개씩 출력 하면 된다.
java.util.Scanner sc = new java.util.Scanner(System.in);
int input = Integer.parseInt(sc.nextLine());
for(int i = 1;i < (1 + input);i++){
System.out.print(i + ",");
}
맞춘 횟수와 틀린 횟수를 기록해서 보여주고 3번 맞추면 프로그램이 종료되게 만들자.
String coinSide = "";
int input = 0;
int success = 0;
int fail = 0;
java.util.random random = new java.util.random();
while(success != 3){
coinSide = random.nextInt(2) == 0 ? "앞면" : "뒷면";
switch(sc.nextLine()){
case "앞면": input = 0; break;
case "뒷면": input = 1; break;
default: fail++; continue;
}
if(coinSide == input){
success++;
} else {
fail++;
}
}
System.out.println("맞춘 횟수:" + success + ", 틀린 횟수:" + fail);
10회 입력받아 맞춘 회수와 틀린 회수를 기록해서 보여 주자.
int fail = 0;
int success = 0;
int diceSide = 0;
int count = 0;
int input = 0;
while(count != 10){
diceSide = random.nextInt(6) + 1;
input = Integer.parseInt(sc.nextLine());
if(diceSide == input){
success++;
} else {
fail++;
}
}
System.out.println("맞춘 횟수:" + success + ", 틀린 횟수:" + fail);