Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-10-27
6
Medium Priority
?
810 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

636 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