- 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 the multiple of x which is closest to a^b in C++
Suppose we have three integers, a, b and x. The task is to get the multiple of x, which is closest to ab. So if a = 5, b = 4 and x = 3, then output will be 624. As 54 = 625, and 624 is the multiple of 3, which is closest to 625.
The task is simple. we have to follow these steps to solve this problem −
- calculate num := ab
- Then find f := floor of (num/x)
- Now the closest element at the left will be cl = x * f, and at right will be cr = x * (f + 1)
- Finally, the closest number among them will be min(num – cl, cr – num)
Example
#include <iostream> #include <cmath> using namespace std; long long getClosest(int a, int b, int x) { long long num = pow(a, b); int f = floor(num / x); long long cl = x * f; long long cr = x * (f + 1); if ((num - cl) < (cr - num)) return cl; else return cr; } int main() { int a = 5, b = 4, x = 3; cout << "Find closest element: " << getClosest(a, b, x); }
Output
Find closest element: 624
- Related Articles
- Find multiple of x closest to or a ^ b (a raised to power b) in C++
- Find number from given list for which value of the function is closest to A in C++
- Find the Closest Palindrome in C++
- Find the value of $(x-a)^3 + (x-b)^3 + (x-c)^3 - 3 (x-a)(x-b)(x-c)$ if $a+b+c = 3x$
- Find K Closest Points to the Origin in C++
- Find the closest leaf in a Binary Tree in C++
- Find k closest elements to a given value in C++
- Find K Closest Elements in C++
- Find First element in AP which is multiple of given Prime in C++
- Which of the following is a factor of $f( x)=x^{2}-9 x+20?$$A). ( x-2)$$B). ( x-3)$$C). ( x-4)$$D). ( x-5)$
- C++ Program to Find Closest Pair of Points in an Array
- Find closest number in array in C++
- Find the closest and smaller tidy number in C++
- In each of the following, find the value of $k$ for which the given value is a solution of the given equation: $x^2-x(a+b)+k=0$, $x=a$
- Find the closest element in Binary Search Tree in C++

Advertisements