

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++
- Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++
- Find if given number is sum of first n natural numbers in C++
- Find the Number of Subarrays whose Minimum and Maximum are Same using C++
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Find a value whose XOR with given number is maximum in C++
- Program to find number of sublists whose sum is given target in python
- Find two numbers whose sum and GCD are given in C++
- Find the first natural number whose factorial is divisible by x in C++
- Find the Number of Unique Triplets Whose XOR is Zero using C++
- Find if array has an element whose value is half of array sum in C++
- Write a Java program to find the first array element whose value is repeated an integer array?
- How do we find out if first character of a string is a number in java?
Advertisements