Thread: C Program that counts total prime number?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    C Program that counts total prime number?

    I need to write c program which would count total numbers of prime between 0 & input value. I cannot use scanf so I must write main function. using command line argument..
    Please Please Help Me

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    if you cannot help
    me
    this is what I wrote so far

    this is the code I wrote
    but the output is wrong
    this one only suppose to post the prime number.

    Code:
    #include <stdio.h>
    
    main( int argc, char *argv[] ) {
    
            int     n=0, i, found_a_factor;
       
       
       
       
            for (n=2; n<=argc; n++) {
                    found_a_factor = 0;
                    for (i=2; i <= n-1; i++)
                            if (n % i == 0) found_a_factor = 1;
                    if (found_a_factor == 0)
                     printf("F(%s) = %s\n",argv[1]);
                //printf ("%d\n", n);
            }
            return 0;
    }

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your for loop should not be going up to argc, that is the count of how many arguments were provided, but you want the value of one of the arguments.
    You're also missing one of the arguments to printf (for the given format string). You need to combine bits of he comented out printf line with the one above it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime number program
    By SVXX in forum C++ Programming
    Replies: 8
    Last Post: 10-03-2008, 11:31 AM
  2. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Largest number program
    By rebel in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 04:20 AM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM