- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

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

Suppose we have the first element of AP, and the differenced. We have to check whether the given number n is a part of AP or not. If the first term is a = 1, differenced = 3, and the term x = 7 will be checked. The answer is yes.

To solve this problem, we will follow these steps −

- If d is 0, and a = x, then return true, otherwise false.
- Otherwise, if d is not 0, then if x belongs to the sequence x = a + n * d, where n is a non-negative integer, only if (n - a)/c is a non-negative integer.

## Example

#include <iostream> using namespace std; bool isInAP(int a, int d, int x) { if (d == 0) return (x == a); return ((x - a) % d == 0 && (x - a) / d >= 0); } int main() { int a = 1, x = 7, d = 3; if (isInAP(a, d, x)) cout << "The value " << x << " is present in the AP"; else cout << "The value " << x << "is not present in the AP"; }

## Output

The value 7 is present in the AP

- Related Articles
- Find The first four terms of an AP, whose first term is $–2$ and the common difference is $–2$.
- Find First element in AP which is multiple of given Prime in Python
- Find First element in AP which is multiple of given Prime in C++
- The sum of the first \( n \) terms of an AP whose first term is 8 and the common difference is 20 is equal to the sum of first \( 2 n \) terms of another AP whose first term is \( -30 \) and the common difference is 8 . Find \( n \).
- The first term of an AP is \( -5 \) and the last term is 45 . If the sum of the terms of the AP is 120 , then find the number of terms and the common difference.
- Find the sum of first 51 terms of an AP whose second and third terms are 14 and 18 respectively.
- If the sum of first p terms of an A.P., is $ap^{2} +bp$, find its common difference.
- The first and the last terms of an AP are 7 and 49 respectively. If sum of all its terms is 420, find its common difference.
- Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++
- Write first four terms of the AP, when the first term $a$ and the common difference $d$ are given as follows:$a = -2, d = 0$
- Write first four terms of the AP, when the first term $a$ and the common difference $d$ are given as follows:$a = 4, d = -3$
- Write first four terms of the AP, when the first term $a$ and the common difference $d$ are given as follows:$a = -1.25, d = -0.25$
- Find if given number is sum of first n natural numbers in C++
- The first and the last terms of an AP are 5 and 45 respectively. If the sum of all its terms is 400, Find its common difference.
- Write first four terms of the AP, when the first term $a$ and the common difference $d$ are given as follows:$a = -1, d = \frac{1}{2}$

Advertisements