Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
C++ code to get two numbers in range x with given rules
Suppose we have a number x. We have to find two integers a and b, such that both of them will be in between 1 and x, a is divisible by b, a * b > x but a/b < x. If not possible, return -1. So, if the input is like x = 10, then the output will be 6 and 3, (other answers are also possible)
To solve this, we will follow these steps −
if x < 2, then: print -1 return print x and x
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h>
using namespace std;
void solve(int x){
if (x < 2){
cout << -1;
return;
}
cout << x << ", " << x;
}
int main(){
int x = 10;
solve(x);
}
Input
10
Output
10,10
Advertisements