Sponsered Links
Categories
Sponsered Links

Factorial in Swing

 

This example illustrates how to make swing program to find the factorial number. In this example you can see that, when user run this program on console then a input box will be appear for entering a number which user want to find factorial. When user enter correctly the integer value then the output will display on the message box.

Source Code of Factorial1.java

import java.io.*;
import javax.swing.JOptionPane;  
public class Factorial1
{
 public static void main(String args[]) 
     throws 
IOException
  {   
     int n,fact=1;
   try   
       {  
   String input = JOptionPane.showInputDialog(
 
"Welcome to Factorial program Enter n: ");
     n = Integer.parseInt(input);
    if(n<0)
    {       
     JOptionPane.showMessageDialog
   (null,
"factorial not possible n="+n);
    }
    else if(n==0)
    {
      JOptionPane.showMessageDialog
    (null,
"factorial of 0=1");
       }
    else if(n>0)
    {
      int tem=n;
          while(n>0)
      {
        fact=fact*n;
        n--;
      }
         JOptionPane.showMessageDialog
    (null,
"factorial of "+tem+"="+fact);
    }

    }catch (Exception e)
       
      JOptionPane.showMessageDialog
   (null,e.getMessage()+
": please inter integer no");
       
           }
  }
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved