2025-02-27
์ค๋์ while
๋ฌธ์ ํ์ฉํ ์ฌ๋ฌ ๊ฐ์ง ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์์ต๋๋ค.
๊ฐ ๋ฌธ์ ์์ while
๋ฌธ์ ์ฌ์ฉํ๋ ๋ฐฉ์๊ณผ ์๊ณ ๋ฆฌ์ฆ์ ์ธ ์ ๊ทผ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ์ต๋๋ค.
import java.util.Scanner;
public class SumMultiplesOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // ์ฌ์ฉ์ ์
๋ ฅ์ ๋ฐ๊ธฐ ์ํ Scanner ๊ฐ์ฒด ์์ฑ
System.out.print("N ์
๋ ฅ: ");
int n = sc.nextInt(); // ์ ์ N ์
๋ ฅ ๋ฐ๊ธฐ
int i = 1, sum = 0; // i๋ 1๋ถํฐ ์์, sum์ 3์ ๋ฐฐ์๋ค์ ํฉ์ ์ ์ฅ
while (i <= n) { // i๊ฐ N ์ดํ์ผ ๋๊น์ง ๋ฐ๋ณต
if (i % 3 == 0) { // i๊ฐ 3์ ๋ฐฐ์์ธ์ง ํ์ธ
sum += i; // 3์ ๋ฐฐ์๋ผ๋ฉด sum์ ๋ํ๊ธฐ
}
i++; // i ๊ฐ์ 1 ์ฆ๊ฐ (๋ค์ ์ซ์๋ก ์ด๋)
}
System.out.println("3์ ๋ฐฐ์ ํฉ: " + sum); // ๊ฒฐ๊ณผ ์ถ๋ ฅ
}
}
import java.util.Scanner;
public class CountMultiplesOfFive {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("์์ ์ซ์ N ์
๋ ฅ: ");
int n = sc.nextInt();
System.out.print("๋ ์ซ์ M ์
๋ ฅ: ");
int m = sc.nextInt();
// ๋ง์ฝ N์ด M๋ณด๋ค ํฌ๋ค๋ฉด ๋ ๊ฐ์ ์๋ก ๋ฐ๊ฟ์ N์ด ํญ์ ์๋๋ก ์ค์
if (n > m) {
int temp = n;
n = m;
m = temp;
}
int i = n, count = 0; // i๋ N๋ถํฐ ์์, count๋ 5์ ๋ฐฐ์ ๊ฐ์ ์ ์ฅ
while (i <= m) { // i๊ฐ M ์ดํ์ผ ๋๊น์ง ๋ฐ๋ณต
if (i % 5 == 0) { // i๊ฐ 5์ ๋ฐฐ์์ธ์ง ํ์ธ
count++; // 5์ ๋ฐฐ์๋ผ๋ฉด count ์ฆ๊ฐ
}
i++; // i ๊ฐ์ ์ฆ๊ฐ (๋ค์ ์ซ์๋ก ์ด๋)
}
System.out.println("5์ ๋ฐฐ์ ๊ฐ์: " + count);
}
}
N
์ด ์์(prime number) ์ธ์ง ํ๋ณํ์ธ์.import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("N ์
๋ ฅ: ");
int n = sc.nextInt();
boolean isPrime = true; // ์ด๊ธฐ๊ฐ์ true๋ก ์ค์
if (n < 2) { // 1 ์ดํ์ ์ซ์๋ ์์๊ฐ ์๋
isPrime = false;
} else {
int i = 2;
while (i < n) { // 2๋ถํฐ N-1๊น์ง ๋๋์ด ๋จ์ด์ง๋์ง ํ์ธ
if (n % i == 0) { // ๋๋์ด ๋จ์ด์ง๋ ์ซ์๊ฐ ์์ผ๋ฉด ์์๊ฐ ์๋
isPrime = false;
break; // ๋ ์ด์ ํ์ธํ ํ์ ์์ผ๋ฏ๋ก ๋ฐ๋ณต๋ฌธ ์ข
๋ฃ
}
i++;
}
}
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
if (isPrime) {
System.out.println(n + "์(๋) ์์์
๋๋ค.");
} else {
System.out.println(n + "์(๋) ์์๊ฐ ์๋๋๋ค.");
}
}
}
N
์ ๋ชจ๋ ์ฝ์๋ฅผ ์ถ๋ ฅํ์ธ์.import java.util.Scanner;
public class Divisors {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("N ์
๋ ฅ: ");
int n = sc.nextInt();
System.out.print(n + "์ ์ฝ์: ");
int i = 1;
while (i <= n) { // 1๋ถํฐ N๊น์ง ๋ฐ๋ณต
if (n % i == 0) { // i๊ฐ N์ ์ฝ์์ธ์ง ํ์ธ
System.out.print(i + " "); // ์ฝ์ ์ถ๋ ฅ
}
i++; // ๋ค์ ์ซ์๋ก ์ด๋
}
}
}
50000์
, 10000์
, 5000์
, 1000์
๋จ์๋ก ์ต์ ๊ฐ์์ ์งํ๋ก ๋ณํํ์ธ์.import java.util.Scanner;
public class ATMWithdrawal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int amount;
System.out.print("์ถ๊ธํ ๊ธ์ก ์
๋ ฅ (1000์ ์ด์): ");
amount = sc.nextInt();
// ์ต์ ๊ธ์ก 1000์ ์ด์์ด์ด์ผ ํจ
while (amount < 1000) {
System.out.println("โ ๏ธ ์ต์ ์ถ๊ธ ๊ธ์ก์ 1000์์
๋๋ค. ๋ค์ ์
๋ ฅํ์ธ์.");
System.out.print("์ถ๊ธํ ๊ธ์ก ์
๋ ฅ (1000์ ์ด์): ");
amount = sc.nextInt();
}
int[] bills = {50000, 10000, 5000, 1000}; // ์ฌ์ฉ ๊ฐ๋ฅํ ์งํ ๋จ์
int i = 0; // ์งํ ๋ฐฐ์ด์ ์ธ๋ฑ์ค
while (i < bills.length) { // ๋ฐฐ์ด ๊ธธ์ด๋งํผ ๋ฐ๋ณต
int count = amount / bills[i]; // ํด๋น ์งํ ๊ฐ์ ๊ณ์ฐ
amount %= bills[i]; // ๋จ์ ๊ธ์ก ์
๋ฐ์ดํธ
if (count > 0) { // ํด๋น ์งํ ๊ฐ์๊ฐ 0๋ณด๋ค ํฌ๋ฉด ์ถ๋ ฅ
System.out.println(bills[i] + "์: " + count + "์ฅ");
}
i++; // ๋ค์ ์งํ ๋จ์๋ก ์ด๋
}
}
}