StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The Loop Mechanic
Download
  1. Java
  2. Programs
The Decision Hub : The Student Blueprint
Programs
Initiative: To analyze number patterns using for and while loops for primality and digit extraction.
public class LoopMechanic { // Define the class for iteration and looping lab
    public static void main(String[] args) { // Standard entry point for the application
        System.out.println("--- Prime Search (2 to 50) ---"); // Print header for the prime number section
        
        for (int i = 2; i <= 50; i++) { // Outer loop to iterate through numbers from 2 up to 50
            boolean isPrime = true; // Assume the current number 'i' is prime initially
            for (int j = 2; j <= Math.sqrt(i); j++) { // Inner loop to check divisors up to the square root
                if (i % j == 0) { // Check if 'i' is perfectly divisible by 'j'
                    isPrime = false; // If divisible, set prime flag to false
                    break; // Exit the inner loop immediately for performance efficiency
                } // End of divisibility check
            } // End of inner divisor loop
            if (isPrime) { // If the flag remained true, the number is prime
                System.out.print(i + " "); // Print the prime number followed by a space
            } // End of the isPrime check
        } // End of the outer range loop
        
        System.out.println("\n\n--- Digit Reversal Lab ---"); // Print header for the number reversal section
        int num = 123456; // Declare an integer to be processed and reversed
        int original = num; // Keep a copy of the original number for the final report
        int reversed = 0; // Initialize a variable to store the reversed result
        
        while (num != 0) { // Run the loop as long as the number has digits remaining
            int digit = num % 10; // Extract the last digit using the modulo operator
            reversed = (reversed * 10) + digit; // Shift existing result and add the new digit
            num = num / 10; // Remove the last digit using integer division
        } // End of the while loop for reversal
        
        System.out.println("Input: " + original); // Display the original number back to the user
        System.out.println("Output: " + reversed); // Display the successfully reversed number
        
        int counter = 5; // Initialize a counter for the do-while demonstration
        do { // Start a do-while loop which guarantees at least one execution
            System.out.println("Countdown: " + counter); // Print the current value of the counter
            counter--; // Decrement the counter by one in each iteration
        } while (counter > 0); // Continue the loop as long as the counter is positive
        
        System.out.println("Mechanic Lab Finished."); // Print final exit message for the program
    } // End of the main method
} // End of the LoopMechanic class
The Decision Hub The Student Blueprint
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website