0

I want to print an array of size n, where each element is pow(i,i),i ranging from 1 to n. i.e. if I input n = 4, it should return me an array A = {1, 4, 27, 256}. This is because power(1,1) = 1, power(2,2) = 4, power(3,3) = 27 and power(4,4) = 256.

But, when I try to run the below code, it is not giving any output.

import java.io.*;
import java.util.*;
import java.lang.Math;

public class Main
{
public static void main(String[] args) 
{
    Main s = new Main();
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    double[] A = new double[n];
    int j ;
    for(j = 0; j <= n; j++)
    {
        A[j] = Math.pow(j+1, j+1);
        //System.out.println(A[j]); --> 1
    }
    System.out.print(A);
    System.out.println(A);  //-->2
    for (int i=0; i<A.length; i++) 
    {
        System.out.print(A[i]+" "); // --> 3
    }
}
}

When I try to remove commented quotes for equation 1, it is printing me the values. But neither of equation 2 or 3 is helping me to print the array.

1
  • 3
    you have an ArrayIndexOutOfBoundsException. change for-loop to for(j = 0; j < n; j++). To print the result you can use System.out.println(Arrays.toString(A)); Commented Mar 14, 2020 at 9:46

3 Answers 3

2

Try This :-

 public static void main(String[] args) {
        Main s = new Main();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double[] A = new double[n];
        int j ;
        for(j = 0; j <= n-1; j++)
        {
            A[j] = Math.pow(j+1, j+1);
            //System.out.println(A[j]); --> 1
        }
        System.out.print(A);
        System.out.println(A);  //-->2
        for (int i=0; i<A.length-1; i++)
        {
            System.out.print(A[i]+" "); // --> 3
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

import java.io.*;
import java.util.*;
import java.lang.Math;

public class Main
{
public static void main(String[] args) 
{
    Main s = new Main();
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    double[] A = new double[n];
    int j ;
    for(j = 1; j <= n; j++)
    {
        A[j-1] = Math.pow(j, j);
        //System.out.println(A[j]); //--> 1
    }
    System.out.print(A);
    System.out.println(A);  //-->2
    for (int i=0; i<A.length; i++) 
    {
        System.out.print(A[i]+" "); // --> 3
    }
}
}

Comments

0

Instead of j <= n;, the loop condition should be j < n because the index start from 0 and ends at n - 1. If you try to access A[n], it will throw array index out of exception.

import java.io.*;
import java.util.*;
import java.lang.Math;

public class Main {

    public static void main(String[] args) {
        Main s = new Main();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double[] A = new double[n];
        int j;
        for (j = 0; j < n; j++) {
            A[j] = Math.pow(j + 1, j + 1);
            //System.out.println(A[j]); --> 1
        }
        System.out.print(A);
        System.out.println(A);  //-->2
        for (int i = 0; i < A.length; i++) {
            System.out.print(A[i] + " "); // --> 3
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.