October 18, 2017

# Write a java program to find Sum of Powers of elements in an array.

Write Java code to find the power of each individual element according to its position index, add them up and return as output.

The return type of the output is an integer which is the sum powers of each element in the array.

** Input and Output Format:**

Input is an integer array.The first element corresponds to the number(n) of elements in an array.The next inputs correspond to each element in an array.

The output is an integer.

**Sample Input 1:**

4

3

6

2

1

**Sample Output 1:**

12

**Sample Input 2:**

4

5

3

7

2

**Sample Output 2:**

61

## Sum of Powers of elements in an array in Java.

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } System.out.println(display(n, a)); } public static int display(int n, int[] a) { { int sum = 0; for (int i = 0; i < n; i++) sum = (int) (sum + Math.pow(a[i], i)); return sum; } } }

One Comment

WRITE A PROGRAM TO ACCEPT A NUMBER N AND PRINT WHETHER IT IS POSITIVE OR NEGATIVE R ZERO