Page 202 - ComputerScience_Class_11
P. 202
The do-while Loop
The Java do-while loop executes a set of statements repeatedly until the specified condition is true. It is used when
the number of iterations is not fixed but the loop needs to be executed at least once irrespective of the test condition.
This is because the test condition is checked after the body of the loop.
The syntax of the do-while loop is:
Initialisation;
do
{
// body of the loop
increment or decrement;
} while (test condition);
For example:
Program 6 Write a program to check whether the given number is a palindrome number or not
(Palindrome number is a number that remains the same on reversing its digits, e.g. 121)
1 import java.util.Scanner;
2 public class PalindromeNumber {
3 public static void main(String[] args) {
4 Scanner sc = new Scanner(System.in);
5 System.out.print("Enter a number: ");
6 int n = sc.nextInt();
7 int r, temp, s = 0;
8 temp = n;
9 do {
10 r = temp % 10;
11 s = s * 10 + r;
12 temp = temp / 10;
13 } while (temp > 0);
14 if (n == s) {
15 System.out.println(n + " is a palindrome number");
16 } else {
17 System.out.println(n + " is not a palindrome number");
18 }
19 sc.close();
20 }
21 }
200 Touchpad Computer Science (Ver. 3.0)-XI

