- Related Questions & Answers
- PHP program to check if a given number is present in an infinite series or not
- C program to find if the given number is perfect number or not
- Check if the given number is Ore number or not in Python
- PHP program to find if a number is present in a given sequence of numbers
- C Program to find the given number is strong or not
- Find if given matrix is Toeplitz or not in C++
- Check if a given number is sparse or not in C++
- Check if given number is Emirp Number or not in Python
- Check if a number is in given base or not in C++
- Java Program to check if a Float is Infinite or Not a Number(NAN)
- Java program to find if the given number is positive or negative
- Program to check whether the given number is Buzz Number or not in C++
- Check whether the given number is Euclid Number or not in Python
- Find if given vertical level of binary tree is sorted or not in C++
- Find the number of players who roll the dice when the dice output sequence is given in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have three integers a, b and c. Suppose in an infinite sequence, a is the first term, and c is a common difference. We have to check whether b is present in the sequence or not. Suppose the values are like a = 1, b = 7 and c = 3, Then the sequence will be 1, 4, 7, 10, …, so 7 is present in the sequence, so the output will be ‘yes’.

To solve this problem, we have to follow these two steps −

When c = 0, and a = b, then print yes, and if a is not same as b, then return no

When c > 0, then for any non-negative integer k, the equation will be b = a + k*c must be satisfied. So (b-a)/c will be a non-negative integer.

#include<iostream> using namespace std; void isBInSequence(int a, int b, int c){ if (a == b) cout << "Yes"; if ((b - a) * c > 0 && (b - a) % c == 0) cout << "Yes"; else cout << "No"; } int main() { int a = 1, b = 7, c = 3; cout << "The answer is: "; isBInSequence(a, b, c); }

The answer is: Yes

Advertisements