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
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

New Topic/Question
Reply




MultiQuote





|