
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Count all possible N digit numbers that satisfy the given condition in C++
- Count subsets 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 triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++
- 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,\ b$ and $c$ such that the following numbers are in A.P. $a,\ 7,\ b,\ 23,\ c$
- Find \( a, b \) and \( c \) such that the following numbers are in AP: \( a, 7, b, 23, c \).
- C# Program to check whether the elements of a sequence satisfy a condition or not
- Find the Number of Sextuplets that Satisfy an Equation using C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Given that one of the numbers of a Pythagorean triplet is 18, find the other two numbers.

Advertisements