Help with program code re prime numbers

This is a discussion on Help with program code re prime numbers within the C Programming forums, part of the General Programming Boards category; Please help! I'm trying to write a function that determines if a number is prime to use this function to ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Unhappy Help with program code re prime numbers

    Please help! I'm trying to write a function that determines if a number is prime to use this function to determine and prints all prime numbers between 1 and 100. This is what I have so far:

    #include <stdio.h>

    int prime(int num)
    {

    int sw = 1;
    int count:

    }

    for( count = num-1; count >=2; count--);

    scanf("%d", &prime);

    printf("%d\n", count, sw=1);
    return 0;
    }
    Anna

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    I could help you with logic. May be u can implement it easily if you know some looping concept.

    If you divide the number from 2 to (number-1), you should get some reminder then that number is prime.

    FYI: 2 is the only even prime number.

    Bye,
    Juganoo

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Code re Prime Numbers

    Thank you Juganoo for your help! I appreciate you!
    Anna

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The code of vVv is almost correct, but it also gives 1 as a prime number, which is incorrect. A number is prime if it has two divisors, 1 has only one divisor. So what you could do is count the number of divisors of a number, if it equals 2, then it is prime.

    Code:
    int nr_of_divisors (int n)
    {
        int i, r = 0;
    
        for (i = 1; i <= n; i++) {
            if ((n % i) == 0)
                r++;
        }
    
        return r;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. detecting numbers in a code of line
    By aama100 in forum C++ Programming
    Replies: 7
    Last Post: 01-23-2008, 10:24 AM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. Need a little help with Cyclic Numbers Program
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 06:01 PM
  4. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21