Write a java program that accepts a given n, then create a 2D array with n rows and n columns. Then for each cell the value should be n+row to the power column. For example if we talk of n=5, then array[0][0] =(5+0)0 = 1. Print out all the values in the array created.
import java.util.*;
class Main {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Input the size of the array: ");
int n=scan.nextInt();
double[][] array = { { Math.pow((n+0),0), Math.pow((n+0),1),Math.pow((n+0),2),Math.pow((n+0),3),Math.pow((n+0),4) },
{Math.pow((n+1),0), Math.pow((n+1),1),Math.pow((n+1),2),Math.pow((n+1),3),Math.pow((n+1),4) },
{Math.pow((n+2),0),Math.pow((n+2),1),Math.pow((n+2),2),Math.pow((n+2),3),Math.pow((n+2),4)},
{Math.pow((n+3),0),Math.pow((n+3),1),Math.pow((n+3),2),Math.pow((n+3),3),Math.pow((n+3),4)},
{Math.pow((n+4),0),Math.pow((n+4),1),Math.pow((n+4),2),Math.pow((n+4),3),Math.pow((n+4),4)}};
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
System.out.println("arr[" + x + "][" + y + "] = "
+ array[x][y]);
}
}
Comments
Leave a comment