Write a java program to find palindrome in the range

Java program to find palindrome in the range. Write a Java program to input two integers, which correspond to the lower limit and upper limit respectively, and find the sum of all palindrome numbers present in the range including the two numbers. Print the sum.

A palindrome number is a number that remains the same after reversing its digits. Example 121 on the reverse will give 121. A single-digit number is not considered a palindrome. In order to start with the sum, first, we will see how to check if the number is palindrome or not.

Check Palindrome Number in Java

Input and Output Format

  • Input consists of 1 integer.
  • The output is printed “It is a palindrome” or “It is not a palindrome”.

Refer sample output for formatting specifications

Sample Input :

131

Sample Output :

It is a palindrome

Algorithm to Check palindrome Number

  • Input a number from the user.
  • Store the number in the temp variable.
  • Now, reverse the number. And, check if the reverse number is the same as that of the original number. 
  • At last, just print the “It is a palindrome” else “It is not a palindrome”.

Program to check Palindrome number in Java

package com.date;

import java.util.Scanner;

public class PalindromeNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int sum = 0, rem;
        int temp = n;
        while(n > 0){
            rem = n % 10;
            sum = (sum * 10) + rem;
            n = n /10;
         }
        if(temp == sum){
            System.out.println("It is a Palindrome");
        }
        else{
            System.out.println("It is not a palindrome");
        }
    }
}

Output

Print the Sum of Palindrome number within a range

Input and Output Format

  • Input consists of 2 integers, which correspond to the lower limit and upper limit respectively.
  • The output consists of an Integer (sum of palindromes).

Refer sample output for formatting specifications

Sample Input :

130

150

Sample Output :

272

(131+141 = 272)

Algorithm to find the sum of palindrome in the range

  • Take two inputs n1 and n2.
  • Create a method that takes 2 inputs n1 and n2 and return the sum of the palindrome.
  • Use a while loop to reverse the numbers within the range.
  • Check the reverse numbers are the same as that number and keep it in the variable sum.
  • At last, just print the value of the sum.

Java program to find the sum of palindrome in the range

package com.demo;
 
import java.util.*;
 
public class Main {
 
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the range:");
int n1 = s.nextInt();
int n2 = s.nextInt();
System.out.println("sum of palindrome nos.within given range is:" + sumOfPalindromeNos(n1, n2));
}
 
public static int sumOfPalindromeNos(int n1, int n2) {
int sum = 0;
for (int i = n1; i <= n2; i++) {
int r = 0, n3 = i;
while (n3 != 0) {
r = (r * 10) + (n3 % 10);
n3 = n3 / 10;
}
// System.out.println(n3);
if (r == i)
sum = sum + i;
}
return sum;
}
}

Output

Palindrome number from 1 to N(or within range)

Input and Output Format

  • Input consists of 2 integers, which correspond to the lower limit and upper limit respectively.
  • The output consists of an Integers

Refer sample output for formatting specifications

Sample Input :

130

500

Sample Output :

Palindrome numbers between this :
131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494

Algorithm to find palindrome in the range

  • Declare variables n1, n2, r1 and r2.
  • Input the range in r1 and r2.
  • Iterate using the loop start with the value in r1 till r2.
  • Use a While loop to reverse a number.
  • Now, just check if the reverse number is that of the number then, just print that number.

Java program to find the palindrome in the range

package com.date;

import java.util.Scanner;

public class PalindromeWithinRange {
    public static void main(String[] args) {
        int r1,r2,n1,n2;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Range :");
        r1 = scanner.nextInt();
        r2 = scanner.nextInt();
        System.out.println("Palindrome numbers between this : ");
        for(int i = r1; i <= r2; i++){
            n1 = i;
            n2 = 0;
            while(n1!=0){
                int rem = n1 % 10;
                n1 = n1 / 10;
                n2 = n2 * 10 + rem;
            }
            if(i == n2){
                System.out.print(i+ " ");
            }
        }
    }
}

Output

Thus, in this way, find the sum of all palindrome numbers along with the printing number present in the range including the two numbers.