Link to home
Start Free TrialLog in
Avatar of crystalfish0318
crystalfish0318

asked on

use while loop to output the numbers and their square between 1 and 10

i'm trying to use while loop to output the numbers and their square between 1 and 10.
Here's my codes:


import java.io.*;
import java.math.*;

public class Square
{
      public static void main(String[] args) throws IOException
      {
            double num = 1;
            while(num <= 10)
            {
            
            num = Math.pow(num, 2);
            System.out.println(num + " ");
            
            }
      }
}

i don't know why my output is
1.0
1.0
1.0
1.0
.
.
.
.
..
it just keeps going forever!!

why?
how do i fix it?
Avatar of sciuriware
sciuriware

Split mind!
increment "num" inside the loop

          double num = 1;
          while(num <= 10)
          {
         
             num = Math.pow(num, 2);
             System.out.println(num + " ");
             num += 1.0
         
          }

ASKER CERTIFIED SOLUTION
Avatar of R6
R6

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi crystalfish0318,
thanks for ur rewarding, glad to know that i can help.
thank you
r6
Avatar of crystalfish0318

ASKER

import java.io.*;
import java.math.*;

public class ch5_num8_partD_SquareNumbers
{
      public static void main(String args[]) throws IOException {
        double num = 1;
        double result;
        while(num <= 10)
        {
              result = Math.pow(num,2);
            System.out.println(result + " ");
            num++;
        }
    }

}

sorry, another question:
how do i make the output horizontally?
hi crystalfish0318,
>>sorry, another question:
Thats fine~

>>how do i make the output horizontally?
As you probably aware, that "println" actually prints the parameter + a \n.  Whereas, "System.out.print" jsut prints out the parameter without a \n being attached at the end.  So in this case, use print instead of println.

thank you
r6