Wednesday, 26 January 2022

A) Write a java program to accept a number from a user, if it is zero then throw user defined Exception “Number is Zero”. If it is non-numeric then generate an error “Number is Invalid” otherwise check whether it is palindrome or not

 Slip 30 Q 1

Save code as         "palindromexception"


import java.util.*;
class ZeroException extends Exception{
    ZeroException(){
        super("Number is 0");
    }
}
class palindromexception{
    public static void main(String[] args) {
        int r,sum=0,temp;
        try{
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            sc.close();
            if(n==0){
                throw new ZeroException();
            }
            else{
                temp=n;
                while(n>0){
                    r=n%10;
                    sum=(sum*10)+r;
                    n=n/10;
                }
                if(temp==sum)
                System.out.println("palindrome number "+temp);
                else
                System.out.println("not palindrome number");
            }
               
        }catch (Exception e){
            System.out.println(e);
        }
    }
}


OUTPUT:



No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...