Java Control Structures Practice Questions
Java Control Structures Practice Questions
To check if a number is a palindrome using 'if-else' and loops, first, use a loop to reverse the digits of the number by repeatedly taking the modulus to extract digits and store them in reverse order. Then, use 'if-else' to compare the reversed number to the original. If they match, it's a palindrome; otherwise, it's not .
To generate the Fibonacci series up to N terms using a loop in Java, initialize the first two numbers of the Fibonacci sequence and use a loop to iterate from the third position up to N, calculating each subsequent number by summing the two preceding numbers. Output each number during iteration to obtain the full series .
To check Armstrong numbers using loops and conditional statements, use a loop to separate individual digits of a number, raise each digit to the power equal to the total number of digits, and sum these values. After calculating the sum, use an 'if' condition to compare this sum to the original number. If they are identical, the number is an Armstrong number. This requires extracting digits, performing power operations, and comparison using 'if-else' .
To implement a password retry system with a three-try limit, use a 'do-while' loop to ask for the password input, and an 'if' condition to check its correctness. Maintain a counter for attempts and increment it with each invalid try. If the counter exceeds three before a correct password is entered, the system should exit or lock further attempts. Use informative prompts to guide the user .
A while loop can be used to count the number of digits by continuously dividing the number by 10 until it reaches zero. Each division operation effectively removes one digit from the end of the number. Increment a counter variable each time the loop runs, which will give the total count of digits in the number once the loop finishes .
To verify if a triangle is valid based on its angles using control statements, sum the three angles. Then, use an 'if-else' conditional structure to check if this sum is exactly 180 degrees. If true, the angles indicate a valid triangle; otherwise, it's invalid .
Printing pyramid patterns using loop structures in Java involves two nested loops: an outer loop to manage the number of rows and an inner loop to print spaces and asterisks. The space loop should decrease as the row number increases, while the asterisk loop prints a progressive number of asterisks, forming a pyramid shape visually. This requires precise control of iteration bounds to achieve symmetry .
Using a 'for' loop to calculate the factorial of a number is significant because it provides a clear, iterative method that initializes a counter to 1 and multiplies it by each integer up to the number itself. This approach is efficient as it ensures all numbers from 1 to the desired value are considered, and it succinctly avoids issues like stack overflow that might arise in recursive methods .
To determine if a year is a leap year using Java control structures, utilize an 'if-else' statement to check two conditions: first, if the year is divisible by 4. If true, then check if the year is not divisible by 100 unless it is also divisible by 400. A leap year is one that satisfies these conditions: divisible by 4, not divisible by 100 unless it is also divisible by 400 .
To determine if a number is divisible by both 5 and 11, apply an 'if-else' structure to check the number against both conditions: the number modulus 5 should be zero, and similarly, the number modulus 11 should also be zero. If both conditions are met, the number is divisible by both 5 and 11 .