BCA Slip19 Java

Java

Slip19

Q.1) 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). [Marks 30] 

Solution

import java.io.*;
class ZeroExc extends Exception
{
  String details;
  ZeroExc(String s)
  {
    details = s;
  }
  public String toString()
  {
   return details;
  }
}

class Statickey
{
 static int n;
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public static void main(String args[])
 {
    int i;
    try
    {
        System.out.print("Enter the Number : ");
    int n=Integer.parseInt(br.readLine());
        if(n==0)
           throw new ZeroExc("Exception : No is Zero");
        int sum = n % 10;
byte r = 0 ;
while(n != 0)
{
r = (byte)(n % 10);
n = n / 10;
}
sum += r;
System.out.println("Sum of first and last digit is : " + sum);
    }
    catch(ZeroExc e)
    {
       System.out.println(e);
    }
    catch(Exception ea)
    {
    }
 }
}

BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post