Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Problem 4: Stop Loop When User Enters Negative Number
1. Start an infinite while loop (while (true)).
2. Ask the user to enter a number. 3. If the number is negative, exit the loop using break. 4. Otherwise, print the number.
Problem 5: Guess the Correct Number
1. Initialize a secret number (e.g., 42).
2. Use a while loop to repeatedly ask the user to guess the number. 3. If the guess is correct, print "Correct!" and exit the loop using break. 4. Otherwise, print "Try again."
Problem 6: Count Digits in a Number
1. Ask the user to input a positive number.
2. Use a while loop to count how many digits the number has. 3. Print the count after the loop ends.
Problem 7: Find the Smallest Power of 2 Greater Than a Given Number
1. Ask the user to input a number N.
2. Use a variable power initialized to 1. 3. Use a while loop to keep doubling power until it is greater than N. 4. Print the final value of power.
Problem 8: Sum of Even Numbers up to 50
1. Initialize a variable num to 1.
2. Use a while loop to check if num is less than or equal to 50. 3. If num is even, add it to a sum variable. 4. Increment num in each iteration. 5. Print the total sum after the loop.
Problem 9: Stop Loop When Sum Exceeds 100
1. Initialize sum to 0 and num to 1.
2. Use a while loop to keep adding num to sum. 3. If sum exceeds 100, exit the loop using break. 4. Otherwise, increment num.
Problem 10: Reverse a Number
1. Ask the user to input a number.
2. Initialize reversed to 0. 3. Use a while loop to extract digits from the number (using % and /) and build the reversed number. 4. Print the reversed number after the loop.