Sherlock and Squares
Problem Statement
Watson gives two integers (A and B ) to Sherlock and asks if he can count the number of square integersbetween A and B (both inclusive).
Note: A square integer is an integer which is the square of any integer. For example, 1, 4, 9, and 16 are some of the square integers as they are squares of 1, 2, 3, and 4, respectively.
Input Format
The first line containsT , the number of test cases. T test cases follow, each in a new line.
Each test case contains two space-separated integers denotingA and B .
Output Format
For each test case, print the required answer in a new line.
Constraints
1≤T≤100
1≤A≤B≤109
Sample Input
Test Case #00: In range[3,9] , 4 and 9 are the two square numbers.
Test Case #01: In range[17,24] , there are no square numbers.
CODE:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b,t,i,j,count=0;
scanf("%d",&t);
while(t!=0)
{
scanf("%d%d",&a,&b);
for(j=0;j<=sqrt(b);j++)
{
if(j*j>=a&&j*j<=b)
{
count++;
}
}
printf("%d\n",count);
count=0;
t--;
}
return 0;
}
REFER LINK:https://www.hackerrank.com/challenges/sherlock-and-squares/
Note: A square integer is an integer which is the square of any integer. For example, 1, 4, 9, and 16 are some of the square integers as they are squares of 1, 2, 3, and 4, respectively.
Input Format
The first line contains
Each test case contains two space-separated integers denoting
Output Format
For each test case, print the required answer in a new line.
Constraints
Sample Input
2
3 9
17 24
Sample output 2
0
Explanation Test Case #00: In range
Test Case #01: In range
CODE:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b,t,i,j,count=0;
scanf("%d",&t);
while(t!=0)
{
scanf("%d%d",&a,&b);
for(j=0;j<=sqrt(b);j++)
{
if(j*j>=a&&j*j<=b)
{
count++;
}
}
printf("%d\n",count);
count=0;
t--;
}
return 0;
}
REFER LINK:https://www.hackerrank.com/challenges/sherlock-and-squares/
No comments:
Post a Comment