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
Following are the steps to sum the power of elements in an array:
- First, input the size of an array.
- Next, add elements to an array.
- Pass both to the display() method.
- Inside the method, declare and initialize the sum variable to 0.
- Now, iterate over an array and find the power of the number using the Math.pow() method by passing the element and its index.
- Add them and store them in a sum variable and return the value of the sum.
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; } } }
Output
Sum of the powers of a digits
Write a code to find the sum of the powers of digits. Consider, Sam was asked to find the sum of the powers of digits as per the below mention method:
- If the number is 123, then Sum should be 12+23+30.
- Each digit is raised to its next digit.
- The rightmost digit has the power 0.
Input and Output Format
- Input is an integer.
- The output is the sum of the powers of an integer.
Sample Input 1:
123
Sample Output 1:
10
Sample Input 2:
54123
Sample Output 2:
639
Java program to find the sum of powers of digits
Following are the steps to find the sum of the powers of the digits:
- Input number from the user.
- Pass the number to the sumOfPower() method.
- Inside the method, return the string object of the specified integer.
- Declare and initialize the sum variable to 0.
- Iterate over the string, get the integer value of the string at i and i+1th position. Now, use the pow() method and it will return the value of the first argument raised to the power of the second argument. And, add them to the sum variable.
- At last, print the value of the sum (+1 is added so that the element which is rightmost will have power 0 and we know that anything raised to 0 is 1)
package com.company; import java.util.Scanner; public class Solution15 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); sumOfPower(n); } private static void sumOfPower(int n) { String s1 = Integer.toString(n); int sum = 0; for(int i = 0;i< s1.length()-1;i++){ int p1 = Character.getNumericValue(s1.charAt(i)); int p2 = Character.getNumericValue(s1.charAt(i+1)); sum = (int) (sum + Math.pow(p1,p2)); } System.out.println(sum+1); } }
Output
Thus, in this way, we learn how to find the sum of the power of a digit using a different approach.