Write a java program to find palindrome in the range

Write a java program to find palindrome in the range. Write a program to input two integers, which corresponds 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.

Note1 : A palindrome number is a number which remains same after reversing its digits.

Note2 : A single digit number is not considered as palindrome.

Input and Output Format:

Input consists of 2 integers, which corresponds to the lower limit and upper limit respectively.

Output consists of an Integer (sum of palindromes).

Refer sample output for formatting specifications.

Sample Input :

130

150

Sample Output :

272

(131+141 = 272)

java program to find 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;
}
}

Add a Comment

Your email address will not be published. Required fields are marked *

3 + 4 =