Q:

C program to find the sum of the Arithmetic Progression (A.P.) series

0

C program to find the sum of the Arithmetic Progression (A.P.) series

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Arithmetic Progression (AP):

A series of numbers is called an arithmetic progression (AP) series if the difference between any two consecutive terms is always the same.

Example: 2 4 6 8 10 ...

In the below program, we will read the first term, the total number of terms, and common difference from the user and print A.P. series and its sum.

Program:

The source code to find the sum of the Arithmetic Progression (A.P.) series is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the sum of
// Arithmetic Progression (A.P.) series

#include <stdio.h>
#include <math.h>

int main()
{
    int sum = 0;

    int a = 0;
    int n = 0;
    int d = 0;
    int i = 0;
    int t = 0;

    printf("Enter the first term: ");
    scanf("%d", &a);

    printf("Enter the value of n: ");
    scanf("%d", &n);

    printf("Enter the difference: ");
    scanf("%d", &d);

    sum = (n * (2 * a + (n - 1) * d)) / 2;
    t = a + (n - 1) * d;

    printf("Sum of the A.P series: ");
    i = a;
    while (i <= t) {
        if (i == t) {
            printf("%d = %d ", i, sum);
            break;
        }

        printf("%d + ", i);

        i = i + d;
    }

    printf("\n");
    return 0;
}

Output:

RUN 1:
Enter the first term: 2
Enter the value of n: 10
Enter the difference: 2
Sum of the A.P series: 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110

RUN2:
Enter the first term: 100
Enter the value of n: 5
Enter the difference: 3
Sum of the A.P series: 100 + 103 + 106 + 109 + 112 = 530 

RUN 3:
Enter the first term: 10
Enter the value of n: 10
Enter the difference: 5
Sum of the A.P series: 10 + 15 + 20 + 25 + 30 + 35 + 40 + 45 + 50 + 55 = 325

Explanation:

Here, we read the first term, common difference, and the total number of terms from the user. Then we generated the A.P. series and find its total and printed them on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now