Number of squares present between two numbers
The following program prints the number of squares present between two numbers.The program is implemented using C++ language.
Ex:
Let the two numbers be 3 and 9.
There are two squares between them i.e. 4 and 9.So the output will be 2.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x,y;
cout<<"Enter two elements : "<<endl;
cin>>x>>y;
x=sqrt(x-1);
y=sqrt(y);
cout<<"\nThe number of squares present : "<<(y-x);
return 0;
}
Description :
In the above program let the two elements be A and B. x is the square root of A and y is the square root of B. The squares of x and y lies in between A and B. Therefore square of elements present between x and y are also present in the range of A and b.
Ex:
5 and 20 are two elements for the program.x=sqrt(5-1)=2 and y=sqrt(20)=4
According to program the count will be 2.9 and 16 are only two square elements present between 5 and 20.
Output:
Case 1:
Enter two elements :3 9
The number of squares present : 2
Case 2:
Enter two elements :
15 24
The number of squares present : 1
Case 3:
Enter two elements :25 49
The number of squares present : 3
Comments
Post a Comment