

- 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 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
- Related Questions & Answers
- Count subsets that satisfy the given condition in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++
- Program to find number of subsequences that satisfy the given sum condition using Python
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Count index pairs which satisfy the given condition in C++
- Find n positive integers that satisfy the given equations in C++
- Find permutation of first N natural numbers that satisfies the given condition in C++
- How to count the number of values that satisfy a condition in an R vector?
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Find the Number of Sextuplets that Satisfy an Equation using C++
- PHP program to find the numbers within a given array that are missing
- Find all pairs (a, b) in an array such that a % b = k in C++
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
Advertisements