i) Ramesh is trying to write a program to check whether a number is palindrome or not. Due to confusion in the syntax of some of the statements. he could not complete the program and has left some places blank marked with ?1?, ?2?, ?3? and ?4? to be filled with expression function. The incomplete program is shown below:

Based on the above discussion. answer the following questions :-

 

a) What expression will be filled in place of ?1?

b) What expression will be filled in place of ?2?

c) What expression will be filled in place of ?3?

d) What expression will be filled in place of ?4?


import java.util.*;

class Palindrome {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int copy, n, rev = 0;
        System.out.println("Enter the number");
        n = sc.nextInt(); 
        copy = n;

        while (n != 0) {
            int r = ?1?; // Place 1
            rev = ?2?;   // Place 2
            n = ?3?;     // Place 3
        }

        if (?4?) {      // Place 4
            System.out.println("Palindrome");
        } else {
            System.out.println("Not palindrome");
        }
    }
}

Single Choice
Views 71

Answer:


import java.util.*;

class Palindrome {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int copy, n, rev = 0;
        System.out.println("Enter the number");
        n = sc.nextInt(); 
        copy = n;

        while (n != 0) {
            int r = n % 10;         // Extract the last digit
            rev = rev * 10 + r;     // Build the reversed number
            n = n / 10;             // Remove the last digit
        }

        if (copy == rev) {          // Compare the original and reversed numbers
            System.out.println("Palindrome");
        } else {
            System.out.println("Not palindrome");
        }
    }
}

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.