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 input=new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n=input.nextInt();
double[][] arr = { { 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 i = 0; i < n; i++)
for (int j = 0; j < n; j++)
System.out.println("arr[" + i + "][" + j + "] = "
+ arr[i][j]);
}
Comments
Leave a comment