Hi guys,I was interesting,how to write a function,to determine the nearest square number that is equal or greater to the length of the string( for example 16 is the next square number after 13)

I don't even know what is the mechanism of this function:-/

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Robert1995

Write a square function,

``````#include <climits>
#include <cmath>

using namespace std;

int square(int q)
{}
int main()
{
int number, min = INT_MAX;
for(int i = 1; i < number/2; ++i) {
if((temp_min = abs(square(i) - number)) <= min) {
min = temp_min;
nearest_s = i;
}
}
cout << nearest_s * nearest_s << endl;
}``````

Do something like this :

``````int nearSqrt(int num){
int n = sqrt(num);
return n*n;
}``````

That returns the nearest square number. For it to be greater than num,you need to return (n+1)*(n+1);

A square number is , a number that can be wrote x^2.For Ex 4^2=16;
How to check this?
1.Read the number , then do n=sqrt(number);
What does "sqrt" do?It simply returns Square root of x.Click me for more information
! Do not forget to use #include <math.h>
2.Show the Answer . pow (nr+1,2);
What does "pow" do ? Read Me.

Problem Solved :

``````#include <iostream>
#include <math.h>

using namespace std;

double Next_pow(double num){
double n=sqrt(num)+1;
return pow(n,2);
}
double number;

int main(){
cin>>number;
cout<<Next_pow(number);
}``````