Java Programs for Basic Algorithms
Java Programs for Basic Algorithms
The Java program lists prime numbers between 1 and a given number by iterating through each number within the range and checking its divisibility. For each number `i`, an inner loop checks divisibility by all preceding numbers starting from 2. If `i` is divisible by any number other than 1 and itself, it is flagged as non-prime. Otherwise, it is printed as a prime number. The complexity is managed by breaking the loop as soon as a divisor is found, thus reducing unnecessary checks .
The program that determines a leap year differs from simple division tests by employing a compound conditional logic that accounts for leap year exceptions. Unlike a basic division test which might only check for divisibility by 4, this program evaluates both divisibility by 400 (indicating a leap year outright) and the combined condition of divisibility by 4 but not by 100. This distinction is crucial as it handles century years systematically, matching the Gregorian calendar rules .
The factorial computation Java program initializes the factorial variable with the target number. It then uses a for-loop starting from one less than the number down to 2, multiplying the current factorial value by the iterator at each step. This iterative multiplication continues until the loop completes, resulting in the factorial of the original number. The concept is based on successive multiplication of all positive integers up to the number itself .
The Java program checks each number in the list to see if it is a palindrome by reversing the number and comparing it to the original number. The reversal process involves dividing the number to extract each digit, which is then appended to a new number variable (`reversedNumber`) using multiplication and addition. This is repeated in a loop until all digits are reversed. If the reversed number equals the original number, it indicates the number is a palindrome .
The Java program generates a Fibonacci series by using an array to store the series elements. Initially, the first two Fibonacci numbers, 0 and 1, are assigned to the first two elements of the array. The program then uses a loop starting from the third position, calculating each Fibonacci number as the sum of the two preceding numbers, i.e., series[i] = series[i-1] + series[i-2]. This continues until the specified limit is reached, populating the array with Fibonacci numbers .
Structured loops contribute to efficient program execution in generating prime numbers by reducing unnecessary computations. The outer loop iterates over potential prime candidates, while the inner loop is used to check divisibility, reducing iterations as soon as a divisor is found by breaking out of the loop. This logic minimizes the number of divisions performed, especially as numbers increase, hence decreasing time complexity. By primarily checking factors up to the square root instead of the number itself in advanced implementations, efficiency could be increased further .
The Java program prints star pyramids by using nested loops. The outer loop controls the number of lines, iterating from 1 up to 5, suggesting the height of the pyramid. For each iteration of the outer loop, an inner loop runs to print the corresponding number of stars in that line, equal to the outer loop's current count. After printing the stars in each inner loop iteration, a newline character is printed to progress to the next line, thus creating a pyramid shape of increasing stars .
The Java program handles edge cases when comparing two numbers by utilizing if-else statements that account for all possible scenarios: whether one number is greater than, less than, or equal to the other. The logic sequentially checks if the first number is greater than the second; if true, it prints the appropriate message. If not, it checks if the first number is less than the second. If neither condition is met, it concludes the numbers are equal, handling all edges of comparison .
The Java program uses conditional statements to determine if a year is a leap year. A year is considered a leap year if it is divisible by 400, or it is divisible by 4 but not divisible by 100. The program checks these conditions using the following logical statement: if (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)). If either condition is true, the year is a leap year .
The rules for the even numbers listing program are simple: iterate a loop from 1 up to a specific limit (e.g., 50), and use the modulo operation `i % 2 == 0` to determine if a number is even. An even number will result in zero when divided by 2. The program prints numbers only when this condition is satisfied, ensuring only even values are outputted between the range .