sum of first n terms = (n / 2) * (2 * a + (n - 1) * d)
![]() |
| Sum of first n terms of an AP (arithmetic progression) |
Now, we will see a java program to find sum of first n terms of an AP. The java program is as follows:
import java.util.Scanner;
public class SumofAP {
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter first term, common difference and n (number of terms)");
int a=sc.nextInt(),d=sc.nextInt(),n=sc.nextInt();
System.out.print("Sum: "+n*(2*a+(n-1)*d)/2);
}
}

No comments:
Post a Comment