JAVA CODE
//Ex12_5 program import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Ex12_5 extends Applet implements ActionListener { private Label lbl; private TextField NumTxt; private int n; public void init() { lbl = new Label ("Enter value of n"); add(lbl); NumTxt = new TextField(3); NumTxt.addActionListener(this); add(NumTxt); } public void paint (Graphics g) { int sum1 = 0, sum2 = 0; sum1 = (n * (n - 1)) / 2; for (int i = 1; i <= n; i++) sum2 = sum2 + i; g.drawString ("The sum of 1 to " + n + " using", 50, 50); g.drawString ("i) formula n(n - 1)/2 is " + sum1, 50, 70); g.drawString ("ii) normal loop is " + sum2, 50, 90); } public void actionPerformed (ActionEvent e) { n = Integer.parseInt (NumTxt.getText()); repaint(); } }