shahnawazm786 / Al_Khozaimi_Batch

Automation Batch
3 stars 1 forks source link

WRITE A JAVA PROGRAM TO CHECK A GIVEN NUMBER IS PALINDROM or Not and also REVERSE the Given number USING METHODS #22

Open Shahzad0123 opened 2 years ago

Shahzad0123 commented 2 years ago

//press 1. for Reverse for a given number. //press 2. for check given number is palindrom or not

package javaprogram;

import java.util.Scanner;

public class ReversePalindrom {

public static void main(String[] args) 
{
    System.out.println("press 1. for Reverse for a given number.");
    System.out.println("press 2. for check given number is palindrom or not.");
    System.out.println("Enter your choice..");
    int ch;
    Scanner s=new Scanner(System.in);
    ch=s.nextInt();
    if(ch==1)
    {
        rev();
    }
    else if(ch==2)
    {
        palindrom();
    }
    else
    {
        System.out.println("Invalid Number...");
    }

}

static void rev()
{
    int r=0,rev1=0;
    System.out.println("Enter any Number..  ");
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    while(n!=0)
    {
        r=n%10;
        rev1=rev1*10+r;
        n=n/10;
    }

    System.out.println("Reverse..> "+rev1); 
}

static void palindrom()
{
    int r=0,rev1=0,org=0;
    System.out.println("Enter any Number..  ");
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    org=n;      //stored original values in org variable
    while(n!=0)
    {
        r=n%10;
        rev1=rev1*10+r;
        n=n/10;
    }
    if(rev1==org)
    {
        System.out.println("Number is palindrom.>  "+org);
    }
    else
    {
        System.out.println(org+" is not palindrom");    
    }
}

}