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
Medium Priority
810 Views
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
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

LVL 24

Expert Comment

ID: 9626015
Split mind!
0

LVL 15

Expert Comment

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

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

Expert Comment

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

Author Comment

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

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

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
Course of the Month11 days, 9 hours left to enroll