Thread: command line argument

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    command line argument

    how to find a factorial of a given number through command line argument

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    can anyone tell me a code to find factorial of a given number throughcommand line argument

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,803
    How to ask a decent question.
    http://www.catb.org/~esr/faqs/smart-questions.html

    If you were expecting an answer on a plate, it doesn't work like that.
    Attention and help on our part depends on some effort on your part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by csit View Post
    can anyone tell me a code to find factorial of a given number throughcommand line argument
    Well, first you need to learn to use the command line arguments, this is actually not very hard at all, try to google for some tutorials on how to use argc and argv.

    Then you just have to write a function that will find the factorial of a given number. This is also not very hard, if you know how to do it on paper it should be a breeze, also, the web is littered with tutorial on recursion that uses factorial calculation as an example.

    Be cautious though, if the user inputs something like 160, the resulting factorial will undoubtedly cause an overflow, so check the input before going on.

    Also, the command line arguments are an array of chars, so you will most likely need to convert it to a number for your function (Unless ofcourse you write it so it will accept a char), look into atoi() for this, or maybe strtol().

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, first you need to learn to use the command line arguments, this is actually not very hard at all, try to google for some tutorials on how to use argc and argv.
    Here's one on this very site. http://www.cprogramming.com/tutorial/c/lesson14.html

    Also, the command line arguments are an array of chars, so you will most likely need to convert it to a number for your function (Unless ofcourse you write it so it will accept a char), look into atoi() for this, or maybe strtol().
    There's a link for every problem. http://faq.cprogramming.com/cgi-bin/...043284385#opt3
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  3. Peculiar malloc / command argument question
    By Beowolf in forum C Programming
    Replies: 4
    Last Post: 09-10-2007, 11:54 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM