Program to find perfect square between 1 and 500 in C
Problem Statement:
Write a program to find a perfect square between 1 and 500 in C using while
loop.
HINT: A number is said to be perfect square if multiples of a number
are same. eg., 16 is perfect square because it's multiples are 4 * 4.
#include <stdio.h>
#include <math.h>
int main()
{
int start=1;
while(start<500){ //change 500 to any n
if (sqrt(start) == (int)sqrt(start)) {
printf("%d ",start);
}
start++;
}
return 0;
}
Here we are going to solve using sqrt() in <math.h>.
sqrt() returns double value. (int)sqrt() returns int value. If
both float and int are same, then it is a perfect square. [i.e., 4.0 == 4] and
print that number.
You can also increase upto any number only by changing condition in While
loop[which is mentioned as comment].
Subscribe us to receive updates instantly.
Please let us know about your views in comment section.
Comments
Post a Comment