Java Programs for Basic Algorithms
Java Programs for Basic Algorithms
To adapt the prime number checker to handle multiple inputs, the program could be modified to accept a list or array of integers instead of a single integer input. Then, it would iterate over each number in the list, applying the same logic to determine primality, and store results in a secondary array or list. Finally, it would output the primality status next to each input number, possibly in a tabular or serialized format . This batch processing would allow for efficient, simultaneous evaluation and display of results.
The program determines if a number is odd or even by reading an integer input and calculating its remainder when divided by 2. If the remainder is 0, it prints "Even"; otherwise, it prints "Odd" . The computational cost of this operation is O(1) because it involves a single arithmetic computation and a conditional check.
The program takes user input for the principal amount (p), the rate of interest (r), and the time period (t), and then uses the formula for compound interest: CI = p * (1 + r/100)^t - p, to calculate and print the compound interest . For handling more sophisticated scenarios, the program could be expanded to include options for different compounding intervals (e.g., monthly, quarterly), support different interest rates for different periods, and incorporate input validation and error handling routines.
The switch-case statement in the program assigns each integer input (from 1 to 7) to a corresponding day of the week by sequentially matching the input with case labels (1 for "Sunday", 2 for "Monday", etc.) and printing the associated day when a match occurs . This provides a clear, direct mapping from numeric input to day output.
The computational efficiency of calculating the sum of natural numbers up to n using a loop is O(n), as it iterates through all numbers from 1 to n summing them . However, the sum can be more efficiently computed using the mathematical formula n(n + 1)/2, which has a constant time complexity, O(1), thereby significantly reducing computational time for large n.
The provided Java program checks if a number is prime by counting the number of divisors it has, with a loop running from 1 to the number itself, and concludes the number is prime if it has exactly two divisors . This is inefficient for large numbers. To optimize, one can check divisibility from 2 to the square root of the number, as any factor larger than its square root would have a complementary factor smaller than the square root, thus reducing the computation time significantly.
The program prints even numbers from 1 to 100 by starting a loop at 2 and incrementing by 2 on each iteration, thus directly stepping through even numbers only until it reaches 100 . Alternative methods could involve using a conditional within a loop that iterates through all numbers between 1 and 100, checking each for evenness, though this would be less efficient.
The program reverses the contents of an array by iterating backwards from the last element to the first, printing each element . Unlike reversing a number, where digits are manipulated via arithmetic operations, array elements are accessed via indexing, which allows direct backward traversal rather than digit extraction.
The algorithm calculates the factorial of a given number by initializing a variable f to 1 and multiplying it by each integer from 1 to n progressively . The time complexity of this approach is O(n) as it involves a single for-loop that iterates n times, where n is the input number.
The program reverses a number by repeatedly taking the last digit (using modulus operation), appending it to a result, and removing this digit from the original number (using integer division) until no digits remain . For someone unfamiliar with programming: imagine peeling off each digit from the end of a number and placing them one by one in a new reversed order.