#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Philippines
    Posts
    2
    Rep Power
    0

    Talking 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    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 argcchar** 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(
    i1<= maxi++)
        {
            
    flag 0;
            
    /*Division is just for square root of the value i
            sufficient to check all the possible*/
            
    for(=<= sqrt(i); j++)
            {
                if(
    i%== 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 08:54 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12

    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/
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    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.
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243

    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".

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    house
    Posts
    2
    Rep Power
    0
    oppps !
    sorry i forgot to add, the range is from 2 to 150 .

    thank for all of you who help me
    thanks again
  12. #7
  13. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14

    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 :-)
  14. #8
  15. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    2-150? 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 non-prime
    If prime, add to list
    end loop
    print list of primes
    Last edited by dog135; August 1st, 2003 at 12:11 PM.

IMN logo majestic logo threadwatch logo seochat tools logo