Wednesday, 2 December 2020

Palindrome using For Loop

Write a Java Program to Check Whether a Number is Palindrome or Not using For Loop  

import java.util.Scanner;
public class PalindromeFor 
{
	public static void main(String[] args) 
	{
		int reminder,reverse=0,temp;
		Scanner scan = new Scanner(System.in);
		System.out.println("\nEnter a Number: ");
		int n = scan.nextInt();
		temp=n;
		for(;n!=0;)
		{
			reminder=n%10;
			reverse=reverse*10+reminder;
			n=n/10;
		}
		if(temp==reverse)
		{
			System.out.println("\n\n"+temp+" is a pallindrome");
		}
		else
		{
			System.out.println("\n\n"+temp+" is not a pallindrome");
		}
	}
}

Output 

Enter a Number: 
12321


12321 is a pallindrome




No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32