printing prime numbers error

i'm trying to print prime numbers between 1 and 100 but all that comes out is 2 and a very large negative number. can you guys please point out any errors in my code?

#include<stdio.h>
#include<math.h>
int main(void)
{
int n, x;
do
{
printf("2 \n");
printf("%d \n", prime(n));
}
while (prime(n)==x);
return 0;
}
int prime(int n, int m, int x)
{
for (n=3; n<=100; n+=2)
{
for (m=2; m<<n; m++)
{
if (n%m==0)
return 0;
else
return x;
}
}
}
neither x nor n is initialized in main() before you use them...
m<<n is a bitshift by n positions; you meant .
and how this compiles is beyond me (must be k&r C) since prime() is not declared before
it is called and it isn't even called with the correct number of parameters.
neither x nor n is initialized in main() before you use them...

by that you mean setting initial values?
so

#include<stdio.h>
#include<math.h>
int main(void)
{
int n=1;
char x;
do
{
printf("2 \n");
printf("%d \n", prime(n));
}
while (prime(n)=='x');
return 0;
}
int prime()
{
int n=1;
int m=1;
char x;
for (n=3; n<=100; n+=2)
{
for (m=2; m<n; m++)
{
if (n%m!=0)
return x;
}
}
}

i got rid of the negative value by doing that, thanks!
but now i get 2 and 8...

m<<n is a bitshift by n positions; you meant .

i changed it to m<n instead, to mean that m goes until 1 less than n. is that right?

since prime() is not declared before it is called and it isn't even called with the correct number of parameters

i removed them (n, m, and x) from prime() and declared them later on. i think that helped, thanks!
Last edited on
Topic archived. No new replies allowed.