# Arithmetic Progression Program in C

## What is Arithmetic Series?

In arithmetic series, the term is calculated by adding the common difference between the two terms in the previous series to arrive at the term for the next series. It is a series, in which the difference between the two consecutive terms is equivalent to each other. (n+1)th term of AP can be calculated by taking tn + D, where tn is the nth term of arithmetic progression.

**Example:**

Let the input be a=2, d=1 , n=5

The output for the above input is 6 because the series for the above example is 2,3,4,5,6 nth term will be 6.

In the similar manner for a=7,d=2,n=3

Here in both cases, a is the first number of the series, and d is the difference between the two numbers. When we add the first number and the given difference, we will get the 2nd number. When we add the 2nd number and the difference, we will get the 3rd number. Similarly, we need to perform the same operation until the given nth number. The output will be 11 as the series will be 7,9,11.

## Algorithm

Now let's see the algorithm of the arithmetic progression program:

**Start****Step 1**: Create a function which returns (a+(n-1)*d)**Step 2**: Create a main function that declare the inputs and print the results returned by the function.**Stop.**

**Example-1:**

Now let's see the code by using the above algorithm:

#include <stdio.h>

int nth_ap(int a, int d, int n) {

//this will return the nth number of the series provided by the main function

return (a + (n - 1) * d);

}

//main function

int main() {

// starting number will be stored in the variable

int a = 7;

// Common difference will be stored in the d variable

int d = 2;

// N th term to be found

int n = 3;

printf("The %dth term of AP :%d", n, nth_ap(a,d,n));

return 0;

}

**Output:**

**Explanation:**

The above code returns the nth value of the series. In the above code, we have defined a function nth_ap which will return the nth value of the arithmetic function. In the main function, we have declared the values a,d, and n. Here a is the first value of the series, d is the difference between the two numbers of the series, and n is the nth number to be found in the series. In the main function, we have passed all the values of the function nth_ap, which will return the nth digit of the series and print it in the main function.

**Example-2:**

Now let's see the same example by taking the input from the user:

#include <stdio.h>

int nth_ap(int a, int d, int n) {

//this will return the nth number of the series provided by the main function

return (a + (n - 1) * d);

}

//main function

int main() {

int a;

int d;

int n;

printf("Enter the first number of the series: ");

// starting number will be stored in the variable

scanf("%d",&a);

printf("Enter the difference of the series: ");

// Common difference will be stored in the d variable

scanf("%d",&d);

printf("Enter the nth digit to be found");

// N th term to be found

scanf("%d",&n);

printf("The %dth term of AP :%d", n, nth_ap(a,d,n));

return 0;

}

**Output:**

**Explanation**:

The above code will return the nth value of the series. In the above code, we have defined a function nth_ap which will return the nth value of the arithmetic function. In the main function, we have declared the values a,d, and n. Here a means the first value of the series, d is the difference between the two numbers of the series, and n is the nth number to be found in the series. We have taken all three values from the user using the scanf() function. In the main function, we have passed all the values of the function nth_ap, which will return the nth digit of the series and print it in the main function.

**Example: 3**

Now, let's see an example that prints the value of the ap series till the given number:

#include <stdio.h>

#include <stdlib.h>

int main() {

int first, diff, terms, value, sum=0, i;

printf("Enter the number of terms in AP series\n");

scanf("%d", &terms);

printf("Enter first term and common difference of AP series\n");

scanf("%d %d", &first, &diff);

/* print the series and add all elements to sum */

value = first;

printf("AP SERIES\n");

for(i = 0; i < terms; i++) {

printf("%d ", value);

value = value + diff;

}

return 0;

}

**Output:**

**Explanation:**

The above code will return the AP series. In the above code, we have taken the input from the user, i.e., the number of terms in the series and the first and difference between the numbers. Using the for loop, we can print the AP series.