Java While Loop Practice Exercises

0% found this document useful (0 votes)
250 views1 page
The document outlines 10 programming exercises to practice using while loops in different scenarios. The exercises include printing numbers from 1 to 10, calculating factorials, finding digi…

Uploaded by

Flori Moise
  • Programming Exercises

Exercise 1: Print Numbers from 1 to 10 Write a program that uses a while loop to print

numbers from 1 to 10.

Exercise 2: Calculate Factorial Write a program to calculate the factorial of a number


entered by the user using a while loop.

Exercise 3: Find the Sum of Digits Create a program that calculates the sum of digits in a
given number using a while loop.

Exercise 4: Reverse a Number Write a program to reverse a number using a while loop.

Exercise 5: Print Even Numbers Print all even numbers from 1 to 50 using a while loop.

Exercise 6: Check for Palindrome Create a program that checks if a given number is a
palindrome (reads the same backward) using a while loop.

Exercise 7: Calculate the Fibonacci Sequence Write a program to generate the first n
Fibonacci numbers using a while loop.

Exercise 8: Prime Number Checker Create a program that checks if a given number is
prime or not using a while loop.

Exercise 9: Print Multiplication Table Generate the multiplication table of a number


entered by the user using a while loop.

Exercise 10: Password Guessing Game Write a simple password guessing game using a
while loop. The program should keep asking the user for a password until they enter the
correct one.

Common questions

Powered by AI

To generate the first n Fibonacci numbers using a while loop, initialize two variables representing the first two numbers in the sequence. Use a counter to keep track of the number of iterations. In each loop iteration, calculate the next Fibonacci number by summing the last two numbers, update the sequence variables accordingly, and increment the counter until it reaches n.

To calculate the sum of digits using a while loop, repeatedly divide the number by 10, adding the remainder to a sum variable in each iteration. Continue until the number reduces to zero. Edge cases include handling zero and negative numbers, which might require additional logic to ensure the summation reflects all digits correctly.

To check if a number is prime using a while loop, first handle edge cases where the number is less than 2. Then, initialize a counter at 2 and loop until the counter is less than the number. Within the loop, check if the number is divisible by the counter; if it is, the number is not prime. If the loop completes without finding a divisor, the number is prime.

Initialize a variable with the starting even number (2). Use a while loop to iterate as long as this variable is less than or equal to 50. In each iteration, print the number and increment it by 2 to continue with the next even number. This process efficiently prints all even numbers from 1 to 50.

When implementing a palindrome check using a while loop, convert the number to its reverse as you iterate through its digits. Compare this reversed number to the original. For numbers of different lengths, the method remains consistent, but thorough testing across significant digits is essential to ensure accuracy. Handling cases with single digits and even versus odd length numbers must be carefully considered.

To reverse a number using a while loop, repeatedly extract the last digit of the number using the modulus operation and add it to a new result variable shifted left by one place. Divide the original number by ten in each iteration to remove the last digit. Continue this process until the original number becomes zero. Challenges include correctly handling leading zeros in the reversed number.

The primary challenge in printing numbers from 1 to 10 using a while loop is ensuring correct initialization and loop termination. Start by initializing a counter at 1 and use a while loop to print the counter as long as it is less than or equal to 10. Increment the counter in each iteration. Ensure termination by not increasing the counter beyond 10 inadvertently.

Implementing a password guessing game involves a loop that continuously prompts the user for input until they enter the correct password. The loop checks each input against the correct password and provides feedback such as 'incorrect, try again'. Considerations include setting up limit attempts, clear instructions, sensitivity to casing (if relevant), and ensuring a robust exit condition if needed.

To calculate the factorial of a number using a while loop, initialize a variable to store the result (starting at 1) and another variable to act as a counter starting at 1. Use the while loop to iterate until the counter exceeds the number. In each iteration, multiply the result variable by the counter, then increment the counter. This process continues until the factorial value is obtained.

To efficiently generate a multiplication table using a while loop, start by setting a counter at 1 and loop while the counter is less than or equal to 10. In each iteration, multiply the input number by the counter, print the result, and increment the counter. This neatly establishes a clear multiplication table up to 10 for any given number.

Exercise 1: Print Numbers from 1 to 10 Write a program that uses a while loop to print 
numbers from 1 to 10.
Exercise 2: Cal

You might also like