How to make have a if statement run after a nested for loop is complete?

Viewed 72

I am wondering if there is a way to run a if loop after a loop is complete without removing it from the loop this is the program I have to do for my com sci class

Write a program that finds the integer from 1 to 1000 with the most factors. For example, 60 has 12 factors: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60. Keep track of the number of factors for each integer from 1 to 1000 in the nested for. When the nested for is done, you program should know which integer has the most factors. Use a separate for loop to output each of the factors. this is the code I have

public class program45{
    public static void main(String args[]){
        int count=0,count1=0,num1=0;
        for (int num=1; num<=1000; num++ ){
            for(int facnum=1; facnum<=num; facnum++){
                if(num%facnum==0){
                    count++;
                }
                if(count>count1){
                    num1=num;
                    count1=count;
                    count=0;
                }
            }
        }
        System.out.println(num1+" has the most factors");
        System.out.print("they are ");
        for(int facnum1=1; facnum1<=num1; facnum1++){
            if (num1%facnum1==0){
                System.out.print(facnum1+", ");
            }
        }
    }
}

already tried putting second if statement in a nested if.

1 Answers
public class program45{
    public static void main(String args[]){
        int count1=0,num1=0;
        for (int num=1; num<=1000; num++ ){
            int count=0
            for(int facnum=1; facnum<=num; facnum++){
                if(num%facnum==0){
                    count++;
                }
            }
            if(count>count1){
                num1=num;
                count1=count;
            }
        }
        System.out.println(num1+" has the most factors");
        System.out.print("they are ");
        for(int facnum1=1; facnum1<=num1; facnum1++){
            if (num1%facnum1==0){
                System.out.print(facnum1+", ");
            }
        }
    }
}
Related