Write a Java program that generates the second, third, and fourth powers of a list of whole numbers from 1 to n where n is input by the user. Write a Java program to do this. • First, ask the user for the largest of the whole numbers to use (n). • Second, output column headers (see below, n, n^2, n^3, n^4). • Then, use a for loop to iterate from 1 to n, computing each of that loop variable to the second power, third power and fourth power, respectively. NB: The values should be stored in a 2dimensional array. Display the values as shown below: Sample output

Respuesta :

Answer:

//import the Scanner class

import java.util.Scanner;

//Begin class definition

public class Powers{

       //Begin the main method

    public static void main(String []args){

       

        //Create an object of the Scanner class

        //to allow for user inputs

       Scanner input = new Scanner(System.in);

       

        //Prompt the user to enter the largest of the whole number

       System.out.println("Enter the largest of the whole numbers to use");

       

        //Receive user input using the Scanner object

        //and store in an integer variable

       int n = input.nextInt();

       //Create a two-dimensional array using the user input

       int [][] powerlist = new int [n][4];

       

       //Create an outer for loop

       //to loop as many times as the number entered by the user

       for (int i = 0; i < n; i++){

           //Create an inner loop to find

           //the powers of each of the numbers

           for(int j = 0; j < 4; j++){

               //Typecast the value returned from the Math.pow() function

               powerlist[i][j] = (int) Math.pow((i+1), (j+1));

               

           }

           

       }

       

       //Print out the header of the table

       //\t will print a tab character.

      System.out.println("n" + "\t\t" + "n^2" + "\t\t" + "n^3" + "\t\t" + "n^4");

       

       //Print out the elements in the two dimensional array

      for (int i = 0; i < powerlist.length; i++){

         

          for(int j = 0; j < powerlist[i].length; j++){

               //Separate each term by two tabs

              System.out.print(powerlist[i][j] + "\t\t");

           }

           

           System.out.println();

           

       }

       

    }

}

Sample Output:

Enter the largest of the whole numbers to use

4

n  n^2  n^3  n^4

1  1  1  1  

2  4  8  16  

3  9  27  81  

4  16  64  256

Explanation:

The code above has been written in Java and it contains comments explaining important lines in the code. Please go through the comments.

For clarity, the actual lines of code have been written in bold face to distinguish them from comments.

Screenshots of the code and a sample output have also been attached to this response.

Ver imagen stigawithfun
Ver imagen stigawithfun

The program is an illustration of loops.

Loops are used to perform repetitive and iterative operations.

The program in Java where comments are used to explain each line s as follows:

import java.util.*;

public class Main{

public static void main(String []args){

   //This creates a scanner object

   Scanner input = new Scanner(System.in);

   //This prompts the user for the largest integer

   System.out.print("Largest:");

   //This gets input for the prompt

   int n = input.nextInt();

   //This prints the output header

      System.out.println("n" + "\t" + "n^2" + "\t" + "n^3" + "\t" + "n^4");

      //This iterates from 1 to n

      for (int i = 1; i <= n; i++){

          //This iterates through the powers

          for(int j = 0; j < 4; j++){

              //This displays each output

              System.out.print((int) Math.pow((i), (j+1))+"\t");

          }

          //This prints a new line

          System.out.println();

      }

   }

}

Read more about similar programs at:

https://brainly.com/question/5419214