- import java.util.Scanner; //import Scanner this function
- public class JPA03 { //make a class "JPA03"
- public static void main(String[] args) { //make a function "main"
- int i, sum; //declare integers
- System.out.printf("1~1000中的完美數有: "); //print out "some things"
- for(i=1; i<=1000; i++){ //the loop for i to run "1 to 1000"
- sum=0; //clear integer sum
- for(int j=1; j<i; j++){ //the loop for getting all common factors
- if(i%j==0) //to check if the integer j is the common factor of i
- sum+=j; //if so, then add j to sum
- }
- if(i==sum){ //to check is i equal to j
- System.out.printf("%d ",i); //if so, then print the number
- }
- }
- System.out.printf("\n"); //print next line(enter)
- }
- }
複製代碼 |