java_program 5

 Practical Name: Write a java program to accept a number from the user, if number is zero then throw user defined Exception “Number is 0” otherwise calculate the sum of first and last digit of a given number (Use static keyword)

___________________________________________________________________________

 

import java.util.Scanner;

class NumberZeroException extends Exception

{

NumberZeroException (int f,int l)

{

System.out.println(f+l);

}

}

class PrimeNumber{

public static void main(String args[])

{

System.out.println("Enter a number");

Scanner sc=new Scanner(System.in);

int a=sc.nextInt();

int first=0;

int last=0;

last=a%10;

if (a == 0) {

System.out.println("number is zero");

} else {

try {

while (a != 0) {

first=a%10;

a=a/10;

}



throw new NumberZeroException(first,last);

} catch (Exception ex) {

System.out.println(ex);

}

}

}

}


Output :-


Post a Comment

Previous Post Next Post