Use java
to find the sum of the following series (up to N terms). The program should display the terms:
22 + 42 + 62…
For example, if N=4, then the program should display the following terms:
22 + 42 + 62 + 82
Sum of terms = 120
(So How I can get 22 + 42 + 62 + 82 for the output . the answer is not only for the sum of terms =120 can you explain how you do it as well?) thank you.
import java.util.Scanner; public class SumNTerms { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a value for n: "); int n = in.nextInt(); int total = 0; for (int i = 1; i <= n; i++) { System.out.print((2*i) + "^2"); if (i != n) { System.out.print(" + "); } total += (2*i)*(2*i); } System.out.println(" = " + total); } }
Get Answers For Free
Most questions answered within 1 hours.