Find the sum of series 1+(1+2) + (1+2+3) +… (1+2+3+…n)
In this program you will learn to create the 1+(1+2) + (1+2+3) +… (1+2+3+…n) sum of series using java programming.
Program code to Find the sum of series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //Find the sum of series 1+(1+2) + (1+2+3) +… (1+2+3+…n). import java.util.Scanner; public class SumofSeries1{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //input number of strings from user System.out.print("Input the value of n : "); int n = scanner.nextInt(); scanner.nextLine(); int i,j,t=0; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { t=t+j; } } System.out.println("Sum of series is :"+ t); } } |
Output
C:\CodeRevise\java>javac SumofSeries1.java
C:\CodeRevise\java>java SumofSeries1
Input the value of n : 4
Sum of series is :20
C:\CodeRevise\java>java SumofSeries1
Input the value of n : 8
Sum of series is :120
Check out our other Java Programming Examples