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
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();
}
}
Comments
Leave a comment