Table of Contents
Java AWT program to print the factorial for an input value
A Java program to print the factorial for an input value with AWT.
Design a AWT program to print the factorial for an input value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import java.awt.*; import java.awt.event.*; class Factorial extends Frame implements ActionListener { TextField tf; Button b; Label n, l, r; Factorial() { n = new Label("AWT Factorial Program"); l = new Label("Enter number"); r = new Label(); tf = new TextField(); b = new Button("Factorial"); //itvoyagers.in n.setBounds(30, 40, 200, 20); l.setBounds(30, 70, 150, 20); r.setBounds(30, 170, 200, 20); tf.setBounds(30, 90, 190, 30); b.setBounds(30, 130, 190, 30); add(n); add(l); add(r); add(tf); add(b); setSize(250,210); setLayout(null);//no layout manager setVisible(true);//now frame will be visible, by default not visible b.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }); } //itvoyagers.in public void actionPerformed(ActionEvent e) { if(e.getSource()==b) { int num = Integer.parseInt(tf.getText()); r.setText("Factorial of "+num+" is "+getFactorial(num)); } } public int getFactorial(int x) { int rsl = 1; for(int i = x; i > 0; --i) { rsl *= i; } return(rsl); } //itvoyagers.in public static void main(String[] args) { Factorial factorial = new Factorial(); } } |
Output

PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.

ITVoyagers
Author