Write a program that displays the squares of the numbers from 1 to 10. Recall th
ID: 3623663 • Letter: W
Question
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.
can you also show me how to show that the codes work?
Explanation / Answer
Here is the code
//Header file
import java.io.*;
class square
{
public static void main(String[] args)
{
System.out.println("Printing Squares using Do while");
squareDoWhile();//function call to print using do while loop
System.out.println("Printing Squares using For loop");
squareFor();//function call to print using for
//exit program
System.exit(0);
}
public static void squareDoWhile()
{
//prints square using do while
int i=0;
do
{
i++;
System.out.println(i+" "+(i*i));
}while(i<=10);
}
public static void squareFor()
{
for(int i=1;i<=10;i++)
System.out.println(i+" "+(i*i));
}
}
Output:
Printing Squares using Do while
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
Printing Squares using For loop
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.