WAP in java to print the sum of the following series : S= 2+6+30+260+ ........ + n terms
Answers
Answer:import java.util.Scanner;
public class Exercise11 {
public static void main(String[] args)
{
int i, n, sum=0;
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
n = in.nextInt();
}
System.out.println("The first n natural numbers are : "+n);
for(i=1;i<=n;i++)
{
System.out.println(i);
sum+=i;
}
System.out.println("The Sum of Natural Number upto "+n+ " terms : " +sum);
}
}
Copy
Sample Output:
Input number: 7
The first n natural numbers are : 7
1
2
3
4
5
6
7
The Sum of Natural Number upto n terms : 28
Flowchart:
Flowchart: Java Conditional Statement Exercises - Display n terms of natural numbers and their sum
Explanation:
Answer:
Sn=n/2(2+an)
Explanation:
Sn=Sum of n term
For ex. : 2+6+30+260.....
n=nth term means that how much terms are there
an=last term