Solved

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

Posted on 2003-10-27
6
807 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 9626015
Split mind!
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9626065
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
ID: 9626068
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

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

Author Comment

by:crystalfish0318
ID: 9632671
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
ID: 9639535
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to configure empty element in XML Document parser? 15 58
wild fly 8 startup error 2 78
ArrayList Adding/settign data 3 37
What is the use of Forwarding Class in java 1 52
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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