- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 numbers a and b that satisfy the given condition in C++
Consider we have an integer n. Our task is to find two numbers a and b, where these three conditions will be satisfied.
- a mod b = 0
- a * b > n
- a / b < n
If no pair is found, print -1.
For an example, if the number n = 10, then a and b can be a = 90, b = 10. This satisfies given rules.
To solve this problem, we will follow these steps −
- Let b = n. a can be found using these three conditions
- a mod b = 0 when a is multiple of b
- a / b < n, so a / b = n – 1 which is < n
- (a * b > n) => a = n
Example
#include<iostream> using namespace std; void findAandB(int n) { int b = n; int a = b * (n - 1); if (a * b > n && a / b < n) { cout << "a: " << a << endl; cout << "b: " << b; }else cout << -1 << endl; } int main() { int n = 10; findAandB(n); }
Output
a: 90 b: 10
Advertisements