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.
Source code
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
System.out.print("Enter n: ");
n=in.nextInt();
int [][] arr = new int [n][n];
for(int row=0;row<n;row++){
for(int column=0;column<n;column++){
arr[row][column]=(int) Math.pow(n + row, column);
}
}
System.out.println("The values of the array are: ");
for(int row=0;row<n;row++){
for(int column=0;column<n;column++){
System.out.print(arr[row][column]+"\t");
}
System.out.println();
}
}
}
Output
Comments
Leave a comment