Answer to Question #309533 in Java | JSP | JSF for Kyle

Question #309533

Write a java program to find the sum of the series up to the nth term where n is input by the user.

1+(1+n)+(1+n+n)+(1+n+n+n)……………….nth Term

Example: input n = 6

Output: 1+7+13+19+25+31 = 96


1
Expert's answer
2022-03-12T12:45:35-0500


import java.util.*;


class App {


	static int sumN(int n, int k) {
		int sum = 1;
		for (int i = 0; i < k; i++) {
			sum += n;
		}
		return sum;
	}


	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter n: ");
		int n = keyboard.nextInt();
		int sum = 1;
		int k;
		System.out.print(sum + "+");
		for (int i = 1; i < n - 1; i++) {
			k = sumN(n, i);
			sum += k;
			System.out.print(k + "+");
		}
		k = sumN(n, n - 1);
		sum += k;
		System.out.print(k + " = " + sum);


		keyboard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS