# non prime numbers or composite numbers program.help plz!

Printable View

• 11-15-2011
danishzaidi
non prime numbers or composite numbers program.help plz!
Code:

```#include <stdio.h> #include <conio.h> int main() { int num; while (num<=100) /*Limit*/ {if(num%2==0)  /*Condition for composite or non prime numbers*/ break; if ((num!=2)); /* as 2 is prime so it is excluded*/ printf("\n%d",num);/* print that non prime numbers*/ num++; /*increment for checking next number*/ } getch (); }```

not giving any output.what i am lacking???
• 11-15-2011
grumpy
Not initialising num, for starters, so it's initial value is indeterminate.

Also breaking out of the loop the first time num is divisible by 2.

getch() and <conio.h> are also non-standard.
• 11-15-2011
danishzaidi
ok i defined num=1
it just printed 1
what should i do with the loop now? i couldn't got any idea about the breaking of loop?
• 11-15-2011
Tibo-88
As grumpy said before, you are breaking the loop when a number is divisible by 2, which means the program will exit the while loop (and if you start with num=1, that will occur when num=2, which is why you have only 1 in output).

If I understand what you want to do, you want to print any non prime number, so you should just have a condition saying "if that number is not divisible by 2 and is not 2" then "print it", this should be enough.
• 11-15-2011
danishzaidi
Quote:

Originally Posted by Tibo-88
As grumpy said before, you are breaking the loop when a number is divisible by 2, which means the program will exit the while loop (and if you start with num=1, that will occur when num=2, which is why you have only 1 in output).

If I understand what you want to do, you want to print any non prime number, so you should just have a condition saying "if that number is not divisible by 2 and is not 2" then "print it", this should be enough.

can you make changes to the code for me.
• 11-15-2011
danishzaidi
Code:

```#include <stdio.h> #include <conio.h> int main() { int num=1; while (num<=100) {if((num%2==0)&& (num!=2)); printf("\n%d",num); num++; } getch (); }```
changed the code now,it is printing prime numbers also.
• 11-15-2011
Tibo-88
You should work a bit in your indentation, it will help you avoiding this kind of mistake. You have a ";" at the end of your if statement that you should remove, because now, if your condition is true, it does nothing, but the printf will be executed no matter what. Also the num<=100 is not so good, you should use num!=101 or num<101 instead.
• 11-15-2011
rac1
Code:

```#include <stdio.h> p(int asal) { int y=1; int i4; for(i4=2;i4<asal;i4++) { if (asal%i4==0) { y=0; } else y=1*y; } return y; } int main() { int i; for(i=0;i<=100;i++) { if((i==0)||(i==1)) printf("%d\n",i); if (p(i)==1) continue; else printf("%d\n",i); } }```

look this. if you want to print all the prime numbers make p(i)==0
• 11-15-2011
Tibo-88
Quote:

Originally Posted by rac1
Code:

```#include <stdio.h> p(int asal) { int y=1; int i4; for(i4=2;i4<asal;i4++) { if (asal%i4==0) { y=0; } else y=1*y; } return y; } int main() { int i; for(i=0;i<=100;i++) { if((i==0)||(i==1)) printf("%d\n",i); if (p(i)==1) continue; else printf("%d\n",i); } }```

look this. if you want to print all the prime numbers make p(i)==0

I didn't run your code, but two things are bothering me while reading it :

Code:

```//FIRST   if(p(i)==1) continue;   else printf("%d\n", i); //can be expressed as   if(p(i)!=1) printf("%d\n", i); //SECOND   y=1*y; //is this really necessary?```
• 11-15-2011
CommonTater
Please indent and format your code correctly.

Quote:

Originally Posted by rac1
Code:

```#include <stdio.h> p(int asal) { int y=1; int i4; for(i4=2;i4<asal;i4++) { if (asal%i4==0) { y=0; } else y=1*y; } return y; } int main() { int i; for(i=0;i<=100;i++) { if((i==0)||(i==1)) printf("%d\n",i); if (p(i)==1) continue; else printf("%d\n",i); } }```

look this. if you want to print all the prime numbers make p(i)==0

• 11-15-2011
rac1
Code:

```#include <stdio.h> p(int asal) { int y=1; int i4; for(i4=2;i4<asal;i4++) {   if (asal%i4==0)  y=0; } return y; } int main() { int i; for(i=0;i<=100;i++) {   if((i==0)||(i==1)) printf("%d\n",i);     if (p(i)==1) continue;     else printf("%d\n",i); } getchar(); return 0; }```
i was using " code blocks" program, there wasnt any error.Now it is working on visual studio 2006 C program. Sorry for that reason.
Thanks Tater, my program is very short and useful now :)