Program to find out the sum of given AP


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Write a program to find out the sum of given A.P.
// Ex: 2 6 2 -> 12; 2 -4 -2 -> -4
// Author: AimForJob.com
#include<stdio.h>
int main(){
    float a,l,d;
    printf("Enter the first number in AP:");
    scanf("%f",&a);
    printf("Enter the last number in AP:");
    scanf("%f",&l);
    printf("Enter the common difference in AP:");
    scanf("%f",&d);
 
    // finding number of terms in the AP
    int n = (int)(l-a)/d+1;
    printf("Total numbers in serires: %d\n",n);
      
    // calculating the sum of AP series
    int i=1;
    printf("\n");
    if(n<=4){
        while(i<n){
            printf("%.2f+",a+(i-1)*d);      
            i++;
        }
        printf("%.2f\n",a+(i-1)*d);
    }
    else{  
printf("%.2f+%.2f+...+%.2f+%.2f\n",a,a+d,l-d,l);
    }
    double sum = (n/2.0)*(a+l);
    printf("\nSum of the series is: %.2f",sum);
}

No comments:

Post a Comment