So I was assigned the task of displaying all numbers divisible by two, five, and every third number in a range which I'm sure you can see how I did that. However I am having trouble as I was asked to use while loops, and I would also like to avoid having to reinitialize the integers start and end every time I create a list but I'm unsure as to how to do this. Any help would be greatly appreciated.

import java.util.Scanner;
public class main
{
    public static void main (String[] args){
        int start, end;
        
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter a starting integer: ");
        start = scan.nextInt();
        System.out.print("Enter an ending integer: ");
        end = scan.nextInt();
        
        System.out.println("All numbers divisible by two in this range are: ");
        for(start = start ; start <= end; start++){
        if(start%2==0){
            System.out.print(start + "\n");
        }
        }
        
        //re-intitializes int start and end
        System.out.print("Please re-enter your starting integer: ");
        start = scan.nextInt();
        System.out.print("Please re-enter your ending integer: ");
        end = scan.nextInt();
        
        System.out.println("All numbers divisible by five in this range are: ");
        for(start = start; start <= end; start++){
        if(start%5==0){
            System.out.print(start + "\n");
        }
        }
        
        System.out.print("Please re-enter your starting integer: ");
        start= scan.nextInt();
        System.out.print("Please re-enter your ending integer: ");
        end = scan.nextInt();
        
        System.out.println("Every third number in this range is: ");
        for(start = start ; start <= end; start+=3){
            System.out.print(start + "\n");
        
    }

   }
}

You need an array of integer to place the number's which are use for division. Like

int dividend[] = {2,5};

Then you need a while loop

while(i<dividend.length)
if(start % a[i])

I have checked your code... Look at this line

for(start = start ; start <= end; start++){

You have done a blunder , try int i instead of start. and i want you to figure it out why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.