Solved

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

Posted on 2003-10-27
6
798 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:crystalfish0318
6 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Split mind!
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
increment "num" inside the loop

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

0
 

Accepted Solution

by:
R6 earned 20 total points
Comment Utility
hi crystalfish0318,

>>why?
Because you didnt increment num.  Therefore, the pow function kept on generating 1 (1^2).

>>how do i fix it?
try:
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++;
        }
    }
It should generate the desired output.

thank you

r6
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:R6
Comment Utility
hi crystalfish0318,
thanks for ur rewarding, glad to know that i can help.
thank you
r6
0
 

Author Comment

by:crystalfish0318
Comment Utility
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?
0
 

Expert Comment

by:R6
Comment Utility
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now