- 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 the Next perfect square greater than a given number in C++
Suppose we have a number n. our task is to find next perfect square number of n. So if the number n = 1000, then the next perfect square number is 1024 = 322.
To solve this, we have get the square root of the given number n, then take the floor of it, after that display the square of the (floor value + 1)
Example
#include<iostream> #include<cmath> using namespace std; int justGreaterPerfectSq(int n) { int sq_root = sqrt(n); return (sq_root + 1)*(sq_root + 1); } int main() { int n = 1000; cout << "Nearest perfect square: " << justGreaterPerfectSq(n); }
Output
Nearest perfect square: 1024
Advertisements