Sum Of Palindrome Elements Of An Array
Write a program to display the sum of palindrome elements in a n x n matrix.
import java.io.*;
class SumOfPalindromes
{
public static void main(String ar[])throws Exception
{
BufferedReader ob=new BufferedReader(
new InputStreamReader(System.in));
int i,j,n,t,r,sum=0;
int a[][]=new int[3][3];
System.out.println("Enter "+(3*3)+" elements ");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
a[i][j]=Integer.parseInt(ob.readLine());
}
}
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
n=a[i][j];
r=0;
while(n>0)
{
t=n%10;
r=r*10+t;
n=n/10;
}
if(r==a[i][j])
sum=sum+a[i][j];
}
}
System.out.println("\nSum of palindrome elements = "+ sum);
}
}
Have something to say? Log in to comment on this post.
0 comments