# Square root Loops

Page 1 of 1

## 6 Replies - 10277 Views - Last Post: 26 November 2008 - 05:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=73695&amp;s=0dbd11473ed5ce58d7a4e46fc6bc5d97&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JynxRD

Reputation: 4
• Posts: 78
• Joined: 06-November 08

# Square root Loops

Posted 26 November 2008 - 04:29 PM

Below is what I've been working with but I'm having a difficult getting it to run and getting it to run correctly. Here are my instructions and this is what (in my mind) I was to do based on those instructions. Hopefully I just need to fix a few things here and there. Any help is greatly appreciated as always!

Quote

Write a program that displays the squares of the numbers from 1 to 10. Recall that the square of a number is just the number multiplied by itself. Demonstrate your knowledge of both looping structures by writing two procedures to do this. One procedure should be called, squareFor and the other should be called, squareDoWhile. Each procedure should take no arguments and return no values. Inside the procedure, simply display a message telling what kind of loop it is using and then use the appropriate loop to display the number, followed by its square.

When you finish writing these two procedures, write a main procedure that will call both squareFor and squareDoWhile to show that they work.

Although the output from your program is not required to look this way, it might look something like:

The squares using a for statement are:
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

The squares using a do...while statement are:
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

// Square root

public class Numbers {
public static void main (String[] args) throws Exception {
System.out.println ("For Loop: ");
For();
System.out.println ("Do While Loop: ");
DoWhile();
}
}

public static void For()
{
int[] numbers = new int[12];
int i;
for (i = 1; i < 11; i++)
numbers[i] = i * i;
for (i = 1; i < 11; i++)
System.out.print("" + numbers[i] + " ");
System.out.print("\n");
for (i = 1; i < 11; i++)
numbers[i] = i;
System.out.print("" + numbers[i] + " ");
System.out.print("\n");
}
}

public static void DoWhile(){
int count = 1;
int countroot = 1;
do {
count = count + 1;
countroot = count * count;
}while (count < 11);
System.out.println(count + " " + countroot);
}
}

This post has been edited by JynxRD: 26 November 2008 - 04:55 PM

Is This A Good Question/Topic? 0

## Replies To: Square root Loops

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Square root Loops

Posted 26 November 2008 - 05:05 PM

you didny have to use arrays...you are just gonna take extra space for nothing

### #3 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Square root Loops

Posted 26 November 2008 - 05:07 PM

What's the output right now?

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Square root Loops

Posted 26 November 2008 - 05:11 PM

this is what you are looking for, works perfectly and exact output as the example
public class loops
{
public static void For()
{
System.out.println("using for loop");
for(int i = 1; i <= 10; i++)
{
System.out.println(i + " " + i*i);
}
}

public static void doWhile()
{
System.out.println("using do while loop");
int i = 1;
do
{
System.out.println(i + " " + i*i);
i++;
}
while(i <= 10);
}

public static void main(String[]args)
{
For();
doWhile();
}
}

### #5 JynxRD

Reputation: 4
• Posts: 78
• Joined: 06-November 08

## Re: Square root Loops

Posted 26 November 2008 - 05:27 PM

I was just getting ready to edit my post. I got to looking at this and thought, why the hell did I use an array? And it was due to the mass amounts of "Java Help" pages I had open and reading I ended up using one of there examples.

mostyfriedman, great that's what I was trying to do. I guess a big question of mine currently is why I kept getting errors on
public static void For() and public static void DoWhile() themselves? For my own learning, was I calling them wrong?

Quote

Numbers.java:12: 'class' or 'interface' expected
public static void For()

This post has been edited by JynxRD: 26 November 2008 - 05:30 PM

### #6 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Square root Loops

Posted 26 November 2008 - 05:32 PM

what errors were you exactly getting?

actually you have an extra brace in the For() method

### #7 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Square root Loops

Posted 26 November 2008 - 05:33 PM

You called them right. You need to include them in a class though...

public class CLASS
{
// not always a main method
public static void main(String[] args)
{
...
}

public static void method1()
{
...
}

public static void method2()
{
...
}
}

Now, they won't always be public static void. There can be infinite types to return, and there are a few other modifiers.

This post has been edited by Locke37: 26 November 2008 - 05:33 PM