July 31st, 2003, 05:35 AM

help!
hello, i'm new !
can somebody please help in c program .
the program is to make a program that will display all prime numbers.:D
July 31st, 2003, 09:51 AM

Hi Jim,
This should not be difficult to program if you understood what a prime number is.
First a basic of what a prime number is , if you already dont know..
prime numbers are numbers that are divisible by 1 and itself.
PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char** argv)
{
int max,flag =0,i,j;
printf("\nEnter the maximum numbers u want prime for:");
scanf("%d",&max);
printf("The prime numbers from 1 to %d are : ",max);
for(i= 1; i <= max; i++)
{
flag = 0;
/*Division is just for square root of the value i
sufficient to check all the possible*/
for(j =2 ; j <= sqrt(i); j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf(" %d",i);
}
}
printf("\n");
fflush(stdin);
getchar();
return 0;
}
Last edited by Raghuraman; July 31st, 2003 at 09:54 AM.
July 31st, 2003, 10:06 AM

Re: help!
Originally posted by sigh
the program is to make a program that will display all prime numbers.:D
This sounds suspiciously like your homework. Since we aren't here to do your homework for you, tell us where you're having the problem and we'll see what we can do.
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/
July 31st, 2003, 12:57 PM

Plus, your subject title is unacceptable. You can refer to the sticky thread at the top of this forum for more information on how to post a question.
July 31st, 2003, 03:41 PM

Not only that...
But what do you mean by 'all prime numbers'? There are an infinite number of them. If you want prime numbers bigger than a value that will fit in your computer's word (probably 32 bits) you need extra libraries (or work in FORTRAN).
Try googling on "Sieve of Eratosthenes".
August 1st, 2003, 03:24 AM

oppps !
sorry i forgot to add, the range is from 2 to 150 .
thank for all of you who help me
thanks again
August 1st, 2003, 08:52 AM

Re: Not only that...
Originally posted by mitakeet
But what do you mean by 'all prime numbers'? There are an infinite number of them. If you want prime numbers bigger than a value that will fit in your computer's word (probably 32 bits) you need extra libraries (or work in FORTRAN).
Try googling on "Sieve of Eratosthenes".
FORTRAN has the same limitations on range of integers it can support as C or C++ (i.e. typically 16, 32, or 64 bits on most systems).
If you want bigger integers in C, C++, or FORTRAN (specifically FORTRAN 90 or later) you are normally looking at user defined types or data structures with associated support functions.
I also assume you made a little typo and meant Sieve of Erastothenes :)
August 1st, 2003, 12:59 PM

2150? Wow, that's a short range.
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
pseudo code:
Code:
setup list of primes
for each number in range:
if number is evenly divisible by any number in list of primes, mark as nonprime
If prime, add to list
end loop
print list of primes
Last edited by dog135; August 1st, 2003 at 01:11 PM.