Programming Examples
Write a program to find the perfect squares from 1 to 500.
Write a program to find the perfect squares from 1 to 500.?
Solution
# include <stdio.h>
#include <math.h>
void main()
{
int i,count,x;
float c;
clrscr();
printf(“\n\n”);
printf(“ Perfect squares from 1 to 500\n”);
count=0;
for(i=1;i<=500;i++)
{
c=sqrt(i);
x=floor(c); /* For rounding up floor() is used. */
if(c==x)
{
printf(“\t%5d”,i);
count++;
}
}
printf(“\n\n Total Perfect Squares =%d\n”,count);
getch();
}
Output
OUTPUT:
Perfect squares from 1 to 500
1 4 9 16 25 36 49 64 81 121 144
169 196 225 256 289 324 361 400 441 484
Total Perfect Squares = 22