First, please use code tags when postig code. See
http://www.cplusplus.com/articles/jEywvCM9/
Second, your code is not a full functional program. We can see that it has flaws, but we can't say they are the only ones.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int n, i, j, p; // uninitialized
j= 2*i+1; // unknown value twice is still unknown
while (cin >>n)
{
if (n%2==0)
{
p=0;
}
else
{
for (i=1; i<=squrt(n);i++)
{
if (n%j != 0)
{
p=0
}
else p=0;
}
}
cout <<p ;
}
|
Lines 13-17 have
IF cond THEN p=0 ELSE p=0 |
No matter what the cond is, you always do p=0. Lets reduce that code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int n, i, j, p; // uninitialized
j= 2*i+1; // unknown value twice is still unknown
while (cin >>n)
{
if (n%2==0)
{
p=0;
}
else
{
for (i=1; i<=squrt(n);i++)
{
p=0;
}
}
cout <<p ;
}
|
Now lines 11-14 set p=0, unless
squrt(n) < 1
.
Furthermore, the j is not used anywhere.
Another clarification:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int n, i, p; // uninitialized
while (cin >>n)
{
if ( n%2==0 )
{
p=0;
}
else if ( 1 <= squrt(n) )
{
p=0;
}
cout <<p ;
}
|
That is what your code essentially does.
We see that if n is even or 1<=squrt(n), then p==0.
For the remaining cases we do not know, because p has no known value before the while-loop.
PS. Why the UNIX/Linux section and not the Beginners section?