Skip to content
Advertisement

Can I run a while loop inside a for loop? [Java] If so, how would I do it in this scenario?

Im doing some simple maths using loops and my task is as follows:

“Given an integer,N , print its first 10 multiples. Each multiple N * i (where 1 <= N <= ) should be printed on a new line in the form: N x i = result.”

My input has to be :

JavaScript

My output:

JavaScript

Sample Input:

JavaScript

Sample Output:

JavaScript

This is what I have done so far:

JavaScript

Would I be able to use a while loop to state the the integer, N, should be able to print out all the numbers in the inclusive range of 1 to 10 to the calculation of the (sample) input: 2×1=2. 2×2=4 etc.

If I can do it without the while loop i.e just using the for loop or for-each, please advise on how I can do it. Im really confused with this. Thanks.

Advertisement

Answer

I really don’t see the problem, you don’t need a while loop at all, you just need simple maths:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement