Lesson 8 of 23

while and do-while

9 min 25 XP

Use while when you don't know how many times to loop. n % 10 is the last digit and n / 10 removes it. These two tricks solve reverse, palindrome-number and Armstrong-number questions.

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

If the condition never becomes false the loop runs forever. Here it is stopped automatically after a few seconds.

Challenge

+25 XP

Read a number. Print "Palindrome" if it reads the same reversed, otherwise "Not palindrome". Input: 1221 → Output: Palindrome

Output Loading C (first time takes a few seconds)
Press Run to see the result here.