# Thread: /i need help in C- program for prime numbers plzZzZ

1. ## /i need help in C- program for prime numbers plzZzZ

Hi everyone how r u ... plz i need help am having exam next week and i have the tips but dont have the answer
anyways this is the question:
1- create a program to print prime number .. and for example it can be all the prime numbers under 200 or under 300 and print ... i hope u understand my question .
plz use normal prog like this
Code:
```#include <stdio.h>
main()
{
here put where ever
}
return 0;```
coz am not good at the other types of program ..
plz try to do it for me as soon as u can or as soon as u see my question ....
thanxx

2. None of us here will do your work for you (this sounds like a homework situation, which there is a policy on this board not to do). But we will try to help you learn how to do it.

The first step is to figure out how you would solve the problem by hand, and write down each step. Once you have that, it shouldn't be THAT hard to write a C-program to solve the problem.

Also, if you haven't done that yet, try writing a program that just prints numbers from one to n where n is (say) 100, 200 or 300.

Then you just have to add "is x a prime then print x" instead of the simple print x in "print all numbers".

This technique of development is called "stepwise refinement", and works quite well for small project like this.

--
Mats

3. You could also google "sieve of eratosthenes" for an algorithm to find prime numbers. But like matsp said, no-one will do your homework for you!

QuantumPete

4. Using sieve if you don't have a good understanding of programming is probably not a particularly good idea - trivial [1] method is much easier to implement.

[1] using the "brute force and ignorance" method is never ideal for REAL work, but since this is intended for relatively small numbers and most likely a teaching excercise early on in a student course, it's probably the BEST method.

--
Mats

5. Fair enough :-D I remember those days at uni, when efficiency was not #1 priority in coding...

QuantumPete

6. I've been noticing this influx of users who use this "i'm a n00b" grammar, it's getting worse, i'm becoming afraid to post here.

i'll have to start going to gamedev more

7. OOOops okay no prob but slowly on me .... i didnt expect someone to do it for me coz i will hav it in exam so i hv to understand it ...i tried my best ok i will tell u wat i did was like wat
i used for statement to print numbers from 1 to 200 and then i just put if statement under it
if (x%2=!0 || x%3=!0 || x%4=!0 ...................||x%9=!0) i mean not equal
printf("%d",a)
but i couldnt continue it worked for %2=! and 3 but the rest didnt work . . . i will try to post wat i did next time coz now i just replied .. thnx anyway..

8. Perhaps you wanted &&, with all of the operators == 0? . . . anyway, that would be much better implemented with a loop. Then you could go
Code:
`if(x &#37; a != 0) { /* ... */ }`

9. Code:
```#include<stdio.h>
main()
{
int n,i,f=0;
printf("enter any number");
scanf("&#37;d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
f=1;
}
if(f==1)
printf("entered number is not a prime");
else
printf("entered number is a prime");
getch();
}```

10. Originally Posted by shuaib.akram
Code:
```#include<stdio.h>
main()
{
int n,i,f=0;
printf("enter any number");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
f=1;
}
if(f==1)
printf("entered number is not a prime");
else
printf("entered number is a prime");
getch();
}```
There we go, the home work is done. lol

ssharish2005

11. Thank you so much!

12. Personally, I'm glad that you found this thread useful, useful enough to express your gratification . . . but you should be aware that you were technically going against the rules of this board.
5. Don't bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
Best of luck with your prime numbers . . .

13. This code is not accurate because if n=2 then f=1, so it will say entered number is not a prime, but it is. Think about it, from here you could go further.

14. Originally Posted by linda
Hi everyone how r u ... plz i need help am having exam next week and i have the tips but dont have the answer
anyways this is the question:
1- create a program to print prime number .. and for example it can be all the prime numbers under 200 or under 300 and print ... i hope u understand my question .
plz use normal prog like this
Code:
```#include <stdio.h>
main()
{
here put where ever
}
return 0;```
coz am not good at the other types of program ..
plz try to do it for me as soon as u can or as soon as u see my question ....
thanxx
I just couldn't help it so i followed the instructions of the student, and because brevity is
a virtue i decided to use a recursive technique for the solution
Code:
```#include <stdio.h>
int main(int a,int n,int i,int j){return a?puts("Enter number:"),scanf("%d",
&n),main(0,n,2,1):(i%j)?main(0,n,i,j-1):i<n?(j==1?printf("%d\n",i):0),main(0
,n,i+1,(i+1)/2):0;}```
There i put wherever between the {} and look what happened !