Print Palindrome Numbers Within Range Java Program

[1412 views]


Palindrome Number:

Palindrome number means the reverse of any number is the same number. Example : 121,323,454 etc.


Print Palindrome Numbers Within Range Java Program

Problem Statement:

Write a program to display all palindrome numbers within a given range.
Steps to solve the program:
  1. Take three inputs range1 and range 2.
  2. Create a for loop to make the iteration process within the boundary and also initialize num1 and num2.
  3. Use while loop to reverse the numbers within the range
  4. Check the reverse numbers are same to that number or not if yes then display within loop.

  1. Take two inputs:
  2. Take two inputs range 1 and range2 for starting the program and also initialize num1 and num2.

    int range1, range2; System.out.println("Enter a range in numbers(num1-num2):"); range1 = sc.nextInt(); range2 = sc.nextInt(); int num1 = 0; int num2 = 0;

  3. Create a for loop to make the iteration process within the boundary:
  4. Create for loop within the range that means i variable started from range1 and ending with range2. The iteration process will continue within that range. Here num1 is replace by I and num2 by 0.

    for (int i = range1; i <= range2; i++) { num1 = i; num2 = 0; }

  5. Use while loop to reverse numbers within the range:
  6. Using while loop and check until num1 is not equal to zero. Store remainder into rem variable, store quotient into num1 and finally store reverse using logic into num2.

    while (num1 != 0) { int rem = num1 % 10; num1 /= 10; num2 = num2 * 10 + rem; }

  7. Check the reverse number is same as that number or not and display the numbers:
  8. By using if statement check whether I means the original number is equal to num2 or not. If yes then display all the numbers

    if (i == num2) System.out.print(i + " "); }

    Full Java Code to Print Palindrome Numbers Within Range:

    import java.util.Scanner; public class GivenRangePalindromeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int range1, range2; System.out.println("Enter a range in numbers(num1-num2):"); range1 = sc.nextInt(); range2 = sc.nextInt(); int num1 = 0; int num2 = 0; System.out.println(range1 + " to " + range2 + " palindrome numbers are"); for (int i = range1; i <= range2; i++) { num1 = i; num2 = 0; while (num1 != 0) { int rem = num1 % 10; num1 /= 10; num2 = num2 * 10 + rem; } if (i == num2) System.out.print(i + " "); } sc.close(); } }

    Console Output:


    Input: Enter a range in numbers(num1-num2): 7894 9000 Output: 7894 to 9000 palindrome numbers are 7997 8008 8118 8228 8338 8448 8558 8668 8778 8888 8998
        

Don't Understand Algorithm and Flowchart? Learn quickly using our Expert's Notes



Want to get in-depth understanding of Algorithms?


Are You Good enough in Algorithms? Test it now?


Comments



Search
Recommended Deals End in










Java Interview Questions For Beginner Notes
Quiz for Learners
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Find Palindrome numbers in a given range using Java