Thread: Any advice?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    15

    Any advice?

    I want to calculate the factorial of a number and have the results printed on the screen. If the number is -1 the program will exit. If the number is less than -1 the program will give an error message and ask for a new number. I've gotten everything taken care of except the whole calculate the factorial and have that result printed? Any advice? Specifics would be appreciated, not generic solutions. Here's what I got so far:

    Code:
    #include <stdio.h>
    
    	int main() {
    
    	   int big, num;
    
    	   num = 5;
    	
    	while (num >= 0) {
    
    	printf("Enter the number.\n");
    	scanf("%d", &big);
    
    	   if (big == -1) {
    	   return 0;
    	   }
    
    	   if (big < -1) {
    	   printf("ERROR - You have just entered a negative number, please enter a non-negative                                    number.\n");
    	   num = 5;
    	   }
    
    	
    	}
    
    	
    
    	}

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Isn't this a repeat of a different post?

    How would you go about calculating factorial if you had to do it with pen and paper. Write down how you would do it, in a way that would explain it step by step, to someone who hasn't got a clue how to do it [but knows how to do the four basic math operations].

    Edit: perhaps one of the moderators can merge this thread with
    http://cboard.cprogramming.com/showthread.php?t=99057
    As it's the same subject and just a bit further along the road - no point in having two threads, I would say.

    --
    Mats
    Last edited by matsp; 02-14-2008 at 05:02 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Wasn't this done using a recursive function?
    One way of doing it I guess is like this?
    Basically it's one line!!


    Code:
    #include <stdio.h>
    int result,number;
    
    main(int  argc, char *argv){
    
    	result=1; //needs to be one as 0 times anything is 0.
    	number=8; //for example
    
    	while(number>=1) result*=number--;// you just need a line like this in your program
    
    	printf("\n%d",result);
    
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by esbo View Post
    Wasn't this done using a recursive function?
    One way of doing it I guess is like this?
    Basically it's one line!!


    Code:
    #include <stdio.h>
    int result,number;
    
    main(int  argc, char *argv){
    
    	result=1; //needs to be one as 0 times anything is 0.
    	number=8; //for example
    
    	while(number>=1) result*=number--;// you just need a line like this in your program
    
    	printf("\n%d",result);
    
    }

    You can certainly do this with recursion, but it doesn't have to be [unless the assignment says so].

    And the general principle in this forum is to not give the answer to assignments.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    mats, seems like you are getting too many PMs for home work help
    *lol*
    Last edited by manav; 02-15-2008 at 04:49 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    Code:
    int main(int  argc, char *argv){
    Main returns int.
    But you don't seem to understand that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    mats, seems like you are getting too many PMs for home work help
    *lol*
    Yes, I think out of the 40 odd PM's I've received, about 80% are "Can you please help me via instant messenger" or "I have this problem, can you please help me". I usually politely point out that I'm not a help-service, and I'm not available all hours [even if it may seem so].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Elysia View Post
    Main returns int.
    But you don't seem to understand that.
    Not to mention the fact that it should be **argv (or *argv[]). Although I would have just left out argc and argv since they're not used in this program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  3. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 01:06 PM
  4. need hardware advice....
    By forgottenPWord in forum Tech Board
    Replies: 5
    Last Post: 03-23-2003, 09:12 AM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM