Wednesday, March 28, 2018

SQUARE AND CUBE A NUMBER USING EXCEPTION IN JAVA


In this article I would like to share with you a program that I wrote in Java that will ask the user to give a number and then our program will compute the square and cube of the given number I added exception handling routine to check for invalid entries.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output


Program Listing

 ExceptionDemo.java

package exceptiondemo;


import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class ExceptionDemo {

   
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
                boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("SQUARE AND CUBE A NUMBER USING EXCEPTION");
                System.out.println("\n");
                System.out.print("Give a Number : ");
int num=sc.nextInt();
                 
                System.out.println("Square of "+ num + " is: "+ Math.pow(num, 2));
System.out.println("Cube of "+ num + " is: "+ Math.pow(num, 3));
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}




No comments:

Post a Comment