Solved

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

Posted on 2003-10-27
6
800 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 73
how to add new optional parameter to JSP 1 29
factorial example 4 35
varialbe initialization 11 30
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

863 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

20 Experts available now in Live!

Get 1:1 Help Now