int n=16;
for(i=0;i<=n;i+开发者_开发知识库+) {
if(n/i==i) {
printf("its a square no");
}
else
printf("not a square no.");
i want to print one statement neither else statement nor if statement.... but output getting not a prime 3 time and prime no one time again...
help me
output for n=16 and n=15
In your code you would also to test that the remainder is 0:
((n/i==i) && (n%i==0))
EDIT
As Sjoerd pointed out, I was being particularly bone-headed and you may as well just test i*i==n
as so:
bool perfect(int n)
{
for (int i=1; i<=n; i++)
if (i*i==n)
return true;
return false;
}
I realise that this is C++ but you get the picture!
Perhaps
int n=16;
for(i=0;1<=n;i++)
should be
float n=16.0;
for(float i=1.0;i<=n;i++)
i=1.0 as you shouldn't divide by zero
you could use found_square=1 when you are successful, and possibly use another if/else statement to print if n is a square, depending on the value of found_square.
also you may like to add a break command following your successful case. As soon as you succeed, there would be no need to continue within the for loop.
精彩评论