- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 the given number is present in the infinite sequence or not in C++
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.
Example
#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); }
Output
The answer is: Yes
- Related Articles
- 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
- PHP program to find if a number is present in a given sequence of numbers
- Check if the given number is Ore number or not in Python
- Find if given matrix is Toeplitz or not in C++
- Check if a given number is sparse or not in C++
- C Program to find the given number is strong or not
- Check if a number is in given base or not in C++
- Check if given number is Emirp Number or not in Python
- Java Program to check if a Float is Infinite or Not a Number(NAN)
- 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 whether a given number is a power of 4 or not in C++
- Find if given vertical level of binary tree is sorted or not in C++
- Check if a number is jumbled or not in C++

Advertisements