Reputation: 13
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;
}
}
Upvotes: 0
Views: 13458
Reputation: 1
I used this code and it also worked
System.out.println("Start");
int number = 9999;
int count = 7;
while (number >= count){
if(number%7==0)
System.out.println(number);
number--;
}}}
Upvotes: 0
Reputation: 1
int count = 9999;
while (count >= 1) {
if (count % 7 == 0) {
System.out.println(count);
}
count--;
}
Upvotes: 0
Reputation: 3196
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;
}
Upvotes: 0
Reputation: 68715
You have a semicolon at the end of your while loop:
while (number <= count);
which will make it an empty loop. And the following curly braces will only act as a code block and not loop
Upvotes: 0
Reputation: 86459
You have an extra semicolon after the while(). Remove it.
With the semicolon at the end of this line, the while loop has an empty body. The following statements in curly braces are executed after the loop has finished. But the loop will never finish, because the condition is always true, because number
never changes.
while (number <= count);
Change it to:
while (number <= count)
{
...
}
Upvotes: 9