# Find if a number is part of AP whose first element and difference are given

Given three non-negative integers a, d and x. Here, a is the first element, d is the difference of an AP (Arithmetic Progression). We need to find if x is part of given AP or not.

**Examples :**

Input : a = 1, d = 3, x = 7 Output : Yes 7 is part of given AP, 1 + 3 + 3 = 7 Input : a = 10, d = 0, x = 10 Output : Yes

Firstly, in case d = 0, we should output Yes if a = x else answer is No. For non-zero d, if x belongs to sequence x = a + n * d where n is non-negative integer, only if (n – a) / c is non-negative integer.

## C++

`// CPP program to check if x exist ` `// or not in the given AP. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// returns yes if exist else no. ` `bool` `isMember(` `int` `a, ` `int` `d, ` `int` `x) ` `{ ` ` ` ` ` `// If difference is 0, then x must ` ` ` `// be same as a. ` ` ` `if` `(d == 0) ` ` ` `return` `(x == a); ` ` ` ` ` `// Else difference between x and a ` ` ` `// must be divisible by d. ` ` ` `return` `((x - a) % d == 0 && (x - a) / d >= 0); ` `} ` ` ` `// Driver code. ` `int` `main() ` `{ ` ` ` `int` `a = 1, x = 7, d = 3; ` ` ` `if` `(isMember(a, d, x)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if x exist ` `// or not in the given AP. ` `class` `GFG { ` ` ` ` ` `// returns yes if exist else no. ` ` ` `static` `boolean` `isMember(` `int` `a, ` `int` `d, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// If difference is 0, then x must ` ` ` `// be same as a. ` ` ` `if` `(d == ` `0` `) ` ` ` `return` `(x == a); ` ` ` ` ` `// Else difference between x and a ` ` ` `// must be divisible by d. ` ` ` `return` `((x - a) % d == ` `0` `&& (x - a) / d >= ` `0` `); ` ` ` `} ` ` ` ` ` `// Driver code. ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `a = ` `1` `, x = ` `7` `, d = ` `3` `; ` ` ` `if` `(isMember(a, d, x)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nikita Tiwari ` |

*chevron_right*

*filter_none*

## Python3

`# Python code to check if x exist ` `# or not in the given AP. ` ` ` `def` `isMember(a, d, x): ` ` ` ` ` `# If difference is 0, then x ` ` ` `# must be same as a. ` ` ` `if` `d ` `=` `=` `0` `: ` ` ` `return` `x ` `=` `=` `a ` ` ` ` ` `# Else difference between x ` ` ` `# and a must be divisible by d. ` ` ` `return` `((x ` `-` `a) ` `%` `d ` `=` `=` `0` `& ` `int` `((x ` `-` `a) ` `/` `d) >` `=` `0` `) ` ` ` `# driver code ` `a ` `=` `1` `x ` `=` `7` `d ` `=` `3` ` ` `if` `isMember(a, d, x): ` ` ` `print` `( ` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by "Abhishek Sharma 44" ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if x exist ` `// or not in the given AP. ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// returns yes if exist else no. ` ` ` `static` `bool` `isMember(` `int` `a, ` `int` `d, ` `int` `x) ` ` ` `{ ` ` ` `// If difference is 0, then x must ` ` ` `// be same as a. ` ` ` `if` `(d == 0) ` ` ` `return` `(x == a); ` ` ` ` ` `// Else difference between x and a ` ` ` `// must be divisible by d. ` ` ` `return` `((x - a) % d == 0 && (x - a) / d >= 0); ` ` ` `} ` ` ` ` ` `// Driver code. ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 1, x = 7, d = 3; ` ` ` `if` `(isMember(a, d, x)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check ` `// if x exist or not in ` `// the given AP. ` ` ` `// returns yes if exist ` `// else no. ` `function` `isMember(` `$a` `, ` `$d` `, ` `$x` `) ` `{ ` ` ` ` ` `// If difference is 0, then ` ` ` `// x must be same as a ` ` ` `if` `(` `$d` `== 0) ` ` ` `return` `(` `$x` `== ` `$a` `); ` ` ` `// Else difference between x ` `// and a must be divisible by d. ` `return` `((` `$x` `- ` `$a` `) % ` `$d` `== 0 && ` ` ` `(` `$x` `- ` `$a` `) / ` `$d` `>= 0); ` `} ` ` ` `// Driver code. ` `$a` `= 1; ` `$x` `= 7; ` `$d` `= 3; ` `if` `(isMember(` `$a` `, ` `$d` `, ` `$x` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Yes

## Recommended Posts:

- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Partiton N into M parts such that difference between Max and Min part is smallest
- Largest subset where absolute difference of any two element is a power of 2
- Minimize sum of adjacent difference with removal of one element from array
- Find N integers with given difference between product and sum
- Program to find the percentage of difference between two numbers
- Find an index such that difference between product of elements before and after it is minimum
- Find the minimum difference between Shifted tables of two numbers
- Program to find the maximum difference between the index of any two different numbers
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Program to find smallest difference of angles of two parts of a given circle
- Find the element that appears once
- Find First element in AP which is multiple of given prime
- Minimum absolute difference of a number and its closest prime
- Program to find the maximum element in a Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.