// // FourthSum.java: prompt user for numbers, raise each to the // fourth power (efficiently), and compute the sum // // No package declarations to keep people from trying to use this lame code.... // import java.util.Scanner; public class FourthSum { // return x to the third power public static int fourthPower(int x) { x *= x; return x * x; } // return sum of fourth powers of given numbers // does not use lambda for campatibility with Java 7 public static int sumOfFourthPowers(int[] numbers) { int sum = 0; for(int n : numbers) sum += fourthPower(n); return sum; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter number(s) on one line: "); String input = in.nextLine().trim(); String[] items = input.split(" +"); // "\\s+" to allow tabs if ( items.length == 1 ) { int n = Integer.parseInt(items[0]); System.out.println("To fourth power: " + fourthPower(n)); } else { int nums[] = new int[items.length]; for(int i = 0; i < items.length; i++) nums[i] = Integer.parseInt(items[i]); System.out.println("Sum of x^4: " + sumOfFourthPowers(nums)); } } }