Saturday, May 15, 2021

Factorial a Number in Java with Error Handling Message

 A simple factorial a number in Java with Error Handling Message.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com




Program Listing

/* Factorial.java
 * Mr. Jake Rodriguez Pomperada,MAED-IT, MIT
 * www.jakerpomperada.com www.jakerpomperada.blogspot.com
 * jakerpomperada@gmail.com
 * Bacolod City, Negros Occidental Philippines
 */
import java.util.Scanner;

public class Factorial {
    public static int factorial (int n){
        int result = 1;
        if (n==0)
            return 1;
        else
            for (int i=1; i<=n; i++) {
                result*=i;
            }
            return result;
    }
    
   
    public static void main(String[] args){
      
        Scanner input = new Scanner(System.in);

        boolean valid;
        
        
        System.out.println();
        do {
        System.out.println();
        System.out.print("\tFactorial Program in Java with Error Handling\n\n");
        System.out.print("\tGive a Number : ");
           int number = input.nextInt();
            valid = (number >= 0   && number <= 16);
            System.out.println();
                
            
            if (!valid) {
            System.out.println("\tNegative Values are Invalid Inputs. Try Again.");
              } 
                   
            else {
              System.out.println("\tThe factorial of " + number +
                  " is " + factorial(number) + ".");
                 
              break;
              }
                
       
        }while (!valid);
        System.out.println();
       System.out.println("\tEnd of Program");
       input.close();
       
      }
}

No comments:

Post a Comment