1. Repeated pattern
// for (initial; condition; update) { code to execute; } // end of for
// print 0 to 9
for (int i = 0; i < 10; i++) {
System.out.println("i : " + i);
} // end of for
// print the only even numbers between 1 to 10
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println("even number : " + i);
} // end of if
} // end of for
// print the total of all the number from 1 to 100
int result = 0;
for (int i = 1; i <=100; i++) {
result += 1;
} // end of for
System.out.println("Total : " + result);
// print the total of all the odd number from 1 to 1000
int result = 0;
for (int a = 1; a <=1000; a++) {
if (a % 2 != 0) {
result += a;
} // end of if
} // end of for
System.out.println("Total : " + result);
// get two values from user and
// print the total of all the odd number from value 1 to value 2
package exercise;
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Give me int number(min) : ");
int num1 = sc.nextInt();
System.out.println("Give me int number(max) : ");
int num2 = sc.nextInt();
int result = 0;
for (int a = num1; a <= num2; a++) {
if (a % 2 != 0) {
result += a;
} // end of if
} // end of for
System.out.println("Total : " + result);
} // end of main
} // end of class
'(Pre-class)' 카테고리의 다른 글
Break and Continue? (0) | 2025.04.02 |
---|---|
While loop? (0) | 2025.04.02 |
Nested if statement? (0) | 2025.03.31 |
If and If else? (0) | 2025.03.31 |
import java.util.Scanner? (0) | 2025.03.30 |