Lab 5 (Solved)

Unit 5

Posted on 4/16/2025

Question 1

public static void simpleWhileLoop() {
    // Define constants
    final int MIN_VALUE = 1;
    final int MAX_VALUE = 10;
    
    // Initialize counter variable
    int num = MIN_VALUE;
    
    // Loop until counter exceeds maximum value
    while (num <= MAX_VALUE) {
        // Print current number
        System.out.println(num);
        
        // Increment counter
        num++;
    }
}

Question 2

private static final int START_NUMBER = 1; // flash
private static final int END_NUMBER = 10; // flash

public static void simpleDoWhile() { // flash
    int counter = START_NUMBER; // flash
    
    do { // flash
        System.out.println(counter); // flash
        counter++; // flash
    } while (counter <= END_NUMBER); // flash
}

Question 3

public void simpleForLoop() {
    // Define constants
    final int START = 1;
    final int END = 10;
    
    // Iterate from START to END and print each number
    for (int i = START; i <= END; i++) {
        System.out.println(i);
    }
}

Question 4

private static final int START_NUMBER = 1; // flash

public static void simpleDoWhile(int upperLimit) { // flash
    int counter = START_NUMBER; // flash
    
    do { // flash
        System.out.println(counter); // flash
        counter++; // flash
    } while (counter <= upperLimit); // flash
}

Question 5

public void simpleForLoop(int upperLimit) { // flash
    final int START = 1; // flash
    
    for (int i = START; i <= upperLimit; i++) { // flash
        System.out.println(i); // flash
    } // flash
}

Question 6

public static void simpleWhileLoop(int upperLimit) { // flash
    final int START_NUMBER = 1; // flash
    
    int counter = START_NUMBER; // flash
    
    while (counter <= upperLimit) { // flash
        System.out.println(counter); // flash
        counter++; // flash
    }
}

Question 7

private static final int QUIT_VALUE = 0; // flash

public static void sumOfEven() { // flash
    Scanner scanner = new Scanner(System.in); // flash
    int sum = 0; // flash
    int number; // flash
    
    do {
        System.out.print("Enter a number (0 to quit): \n"); // flash
        number = scanner.nextInt(); // flash
        
        if (number % 2 == 0 && number != QUIT_VALUE) { // flash
            sum += number;
        }
    } while (number != QUIT_VALUE); // flash
    
    System.out.println("The sum of even numbers is " + sum); // flash
    scanner.close();
}

Question 8

public static void sumOfOdd() {
    int number;
    int sum = 0;

    // Create scanner
    Scanner input = new Scanner(System.in);

    // Start of do-while loop
    do{
        // Number input
        System.out.print("Enter a number (0 to quit): \n");
        number = input.nextInt();

        // Check if the number is odd
        if (number % 2 == 1) {
            sum += number; // Add the odd number to the sum
        }

    } while (number != 0); // Loop until 0 is entered

    // Print the sum of odd numbers
    System.out.println("The sum of odd numbers is " + sum);
}