Number of squares present between two numbers

The following program takes two integers as input and prints number of perfect squares present between those two numbers.
Ex:
Let two numbers be 5 and 16
In this case 2 will be printed as two perfect squares, 9 and 16 are present in between 5 and 16.

#include<stdio.h>
#include<math.h>
int main()
{ 
 int x,y,r;
 
  printf("Enter two numbers :"); 
  scanf("%d %d",&x,&y);
  x=sqrt(x-1);
  y=sqrt(y);
  printf("The number of squares present : %d",(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

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java