/* Write a program to calculate the sum in Arithmetic Progression */
#include<stdio.h>
#include<math.h>
int main() {
    int a, d, n, i, tn;
    int sum = 0;
    printf("Enter the first number of the AP: ");
    scanf("%d", &a);
    printf("Enter the total numbers in the AP: ");
    scanf("%d", &n);
    printf("Enter the common difference of AP: ");
    scanf("%d", &d);
    sum = (n * (2*a + (n-1) * d)) / 2;
    tn = a + (n-1) * d;
    printf("Sum of the numbers in the AP: ");
    for(i=a; i<=tn; i=i+d ) {
        if (i != tn)
            printf("%d + ", i);
        else
            printf("%d = %d ", i, sum);
    }
    getch();
    return 0;
}
Tuesday, February 10, 2015
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment