Programming Examples
Java program to find the sum of the following series
Write a program to find the sum of the following series:
S=1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)+…+(1+2+3+4+…+10)
(Please note that no nested loop is to be used)
Solution
import java.util.*;
class SUM
{
public static void main(String arr[])
{
int i,s=0,p=0;
for(i=1;i<=10;i++)
{
p=p+i;
s=s+p;
}
System.out.println("Sum="+s);
}
}
Output