-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumPalindrome.java
More file actions
30 lines (29 loc) · 832 Bytes
/
SumPalindrome.java
File metadata and controls
30 lines (29 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
public class SumPalindrome {
public static int getPalindromeSum(List<Integer> list){
int sum=0;
Iterator<Integer> itr=list.iterator();
while(itr.hasNext()){
int num=itr.next();
String n=String.valueOf(num);
StringBuffer n1=new StringBuffer(n);
n1.reverse();
String nRev=String.valueOf(n1);
if(n.equals(nRev)){
sum=sum+Integer.parseInt(n);
}
}
return sum;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
List<Integer> list=new ArrayList<Integer>();
System.out.println("Enter from and to range..");
int f=s.nextInt(), t=s.nextInt();
for(int i=f; i<=t; i++){
list.add(i);
}
int sum=getPalindromeSum(list);
System.out.println("\nSum of palindrome numbers between "+f+" and "+t+" -> "+sum);
}
}