Wednesday 20 January 2016

Perfect Squares (Solution to week 1 Assignment)

Write a program to find the number of perfect squares between given two numbers A and B (both inclusive). A number is called a perfect square if it can be written as x*x for some integer x.
Constraints:
Both A and B are positive. They both are less than 100,000.
Input: Two numbers A and B separated by a space
Output: Count of the number of perfect squares
Example 1:
Input: 3 10
Output: 2
Example 2:
Input: 16 70
Output: 5

Solution-:

  #include<stdio.h>
  int main()
{

  int a,b,i,d=0;
  
  scanf("%d",&a);
  scanf("%d",&b);
for(i=1;i<=b;i++)
 {
   if((i*i)>=a && (i*i)<=b)
     d++;
 }
  printf("%d",d);
return 0;

}

No comments:

Post a Comment