- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfect.java
More file actions
Latest commit
70 lines (56 loc) · 1.88 KB
/
Copy pathPerfect.java
File metadata and controls
70 lines (56 loc) · 1.88 KB
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
importjava.awt.event.*;
importjava.awt.*;
classPerfectextendsFrameimplementsActionListener {
Buttonresult;
Labelnumber, r, heading;
TextFieldnum, res;
Perfect() {
super("PERFECT NUMBER");
this.result = newButton("RESULT");
this.number = newLabel("NUMBER");
this.r = newLabel("RESULT");
this.heading = newLabel("PERFECT NUMBER CALCULATOR");
this.num = newTextField(10);
this.res = newTextField(10);
this.add(this.result);
this.add(this.number);
this.add(this.r);
this.add(this.heading);
this.add(this.num);
this.add(this.res);
this.setLayout(null);
this.heading.setBounds(20, 50, 250, 20);
this.number.setBounds(20, 70, 150, 20);
this.num.setBounds(20, 90, 150, 20);
this.r.setBounds(20, 110, 150, 20);
this.res.setBounds(20, 130, 150, 20);
this.result.setBounds(200, 90, 150, 30);
this.setVisible(true);
this.setBackground(java.awt.Color.RED);
this.setSize(500, 300);
this.result.addActionListener(this);
}
publicvoidactionPerformed(ActionEventae) {
intn, temp, sum = 0, sum1 = 0, r, i;
n = Integer.parseInt(this.num.getText());
temp = n;
for (i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
if (sum == temp) {
this.res.setText(Integer.toString(temp) + " is a perfect number");
} else {
while (n > 0) {
r = n % 10;
sum1 = sum1 + r;
n = n / 10;
}
this.res.setText(Integer.toString(sum1));
}
}
publicstaticvoidmain(Stringargs[]) {
Perfectp1 = newPerfect();
}
}