Roger Hamblin Roger Hamblin - 1 month ago 6
Java Question

Writing a while loop to find multiples of 7 from 1 to 9999

I am trying to write a while loop like the question indicates that returns the multiples of 7 in decreasing order. At first I was trying to just write a code to return the values before I made then decreasing, but my while loop won't execute. I added a statement printing "Start" just to make sure it was running.

System.out.println("Start");
int number = 7;
int count = 9999;
while (number <= count);
{
System.out.print(number);
number = number + 7;
}


I wrote it this way to be simpler and because I was unsure of how to make number into a string of values and check each one. Any help on this is appreciated. Thank you!

**RESOLVED. Sorry first time on site and I am not sure if there is another way to close this, but thanks to multiple users point out the semi colon and Vikas pointing out the println error, the code runs. As to making it decreasing I just swapped a few things around:

System.out.println("Start");
int number = 9999;
int count = 7;
while (number >= count)
{System.out.println(number);
number = number - 7;
}


}

Answer

You are not able to see the output value for number because it is println and not print

System.out.print(number);

Change this to

System.out.println(number);

And also as others have answered, remove the semi colon ; at the end of while loop.

Having said this, since you want the result to be printed from Descending to Ascending use below code,

System.out.println("Start");
int number = 7;
int count = 9996;       
while (number <= count)
    {
        System.out.println(count);
        count = count - number;
    }