Palindrome program using For Loop

public class PalindromeProgram {

public static void main(String[] args) {

int n=1234521, rev=0, rem, temp;

temp = n;

for( ;n != 0; n /= 10 )

{

rem = n % 10;

rev= rev* 10 + rem;

}

// palindrome if temp and sum are equal

if temp== rev)

System.out.println(temp + " is a palindrome.");

else

System.out.println(temp + " is not a palindrome.");

}

}



Output: 1234521 is not a palindrome

Explanation: In the above program, the number is not a palindrome. The logic remains the same, just 'for' loop is used instead of a while loop. On each iteration, num /= 10 is executed and condition num!=0 is checked.