Like Tree7Likes

Factorial using a while loop

This is a discussion on Factorial using a while loop within the C Programming forums, part of the General Programming Boards category; Hello guys. I have a problem in my code.... Because i'm a noob. Code: #include <stdio.h> #include <stdlib.h> #include <math.h> ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Factorial using a while loop

    Hello guys. I have a problem in my code.... Because i'm a noob.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    
    
    int main()
    {
    	int n = 0, k = 0, combinations, n_total= 0, loop_count;
    	printf("Enter in a interger\n");
    	scanf("%d", &n);
    
    
    getchek:
    	if(n > 10)
    	{
    		printf("ERROR: Enter a number between 1-10\n");
    		scanf("%d", &n);
    		goto getchek;
    
    
    	} 
    
    
    	else {
    		printf("Enter another interger: \n");
    		scanf("%d", &k);}
    getchek2:
    	if(n < k)
    	{
    		printf("ERROR: Enter a number between 1 and %d\n", n);
    		scanf("%d", &k);
    		goto getchek2;
    	} else 
    	{
    		loop_count=n;
    		n_total=n;
    		while(loop_count=0)
    		{
    			n_total=n_total * n;
    			n_total--;
    			loop_count--;
    		}
    		printf("N = %d", n_total);
    
    
    
    
    
    
    	} 
    
    
    	system("pause");
    	return 0;
    
    
    
    
    }
    My problem seems to be that n is staying at 5 even with my aritmetic.
    Any hints would be helpful.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    This is wrong:
    Code:
    while(loop_count=0)
    You are assigning 0 to loop_count instead of comparing. A for loop will likely be better.

    Also, get rid of your use of goto: loop constructs should have been used instead. There may come a point where you can use goto and defend its use against detractors, but as long as you would say "i'm a noob" because you really are one, you're not qualified to use goto.
    Salem and rogster001 like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    I find that a lot of people on this forum are judgmental, which you seem to be no different.
    My teacher specified that i need to include selection statements, which means i had to use a goto statement. Either way it works, so it's rather moot. I understand that you are more proficient than I, so I'll just take it as a misunderstanding.
    Now that that's out of way.

    I should probably use a for loop, i'll try to incorporate that instead.
    Would it look like
    Code:
    for(loop_counter=n; loop_counter == 1; loop_counter--)

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
            while(loop_count=0)  // better woulld be loop_count>0
            {
                n_total=n_total * n;
                n_total--; // what's that good for
                loop_count--;
            }
    You have initialized n_total to 0. Multiplication won't change the result.

    Kurt
    Last edited by ZuK; 05-11-2013 at 01:51 PM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Yeah i saw that after i posted it. If you look at my revision i used the logical expression ==.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    it would be easier (as pointed out) to use a for loop to compute a factorial - just decrement the loop.

    maybe something like this.. which is the first thing that came to mind...it is only a starting idea - you will find there are bugs..
    Code:
    #include <stdio.h>
    
    int GetFactorial(int n)
    {
    
        int f = n;
        int tmp = 0;
        int i = 0;
    
        for(i = n; i > 1; i--)
        {
            tmp = f * (i-1);
            f = tmp;
        }
    
        return tmp;
    }
    
    int main()
    {
        int n = 6;
        int factorial = 0;
    
        factorial = GetFactorial(n);
    
        printf("%d\n", factorial);
    
        return 0;
    
    }
    Last edited by rogster001; 05-11-2013 at 02:25 PM. Reason: changed to C
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    I'm fairly new to C, but this looks like C++ - std::cout.

    I'm working on the code now, thanks for all your guys input so far!

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    o no, so sorry - forgot was writing in the C forum.. i will amend, but am sure you see the idea in the interim!
    Last edited by rogster001; 05-11-2013 at 02:24 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    I find that a lot of people on this forum are judgmental, which you seem to be no different.
    hmmm... there is some interesting reading on why it might seem that way, in this link
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by sourpatchkid View Post
    My teacher specified that i need to include selection statements, which means i had to use a goto statement.
    Nonsense.
    There is no excuse for using a goto here other than your inability or unwillingness to learn the proper flow control alternatives.
    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"

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Nonsense.
    There is no excuse for using a goto here other than your inability or unwillingness to learn the proper flow control alternatives.

    We actually went over that part in my class, so you can think whatever you may. But i know what my teacher wants, and i'm not going to budge. I understand what laser said. That yes, i can use a loop. But using a selection statement is what my assignment specify's. I have to use selection statements for educational purposes, just like i don't have to use loops to find out factorials. I could of just used a pre-made library if your going to get technical.
    Last edited by sourpatchkid; 05-11-2013 at 03:27 PM.

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    hmmm... there is some interesting reading on why it might seem that way, in this link

    Considering the fact that you gave me source code in c++, i don't know if i should take you seriously.
    I asked why my while loop was not changing my variable N, so i thought that the question was rather obvious.
    So i give you this:How to Be Respectful: 11 Steps (with Pictures) - wikiHow

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    but am sure you see the idea in the interim!
    For the most part, yes.

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sourpatchkid View Post
    .. just like i don't have to use loops to find out factorials.
    So why are you using a loop ?
    Kurt
    AndiPersti likes this.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    So why are you using a loop ?
    Kurt
    "The assignment is to create a program to display the number of combinations where n and k are entered by the user. Do not use the built-in C function to compute either combinations or factorials - do the arithmetic in your program!"
    This comes straight out of the assignment instructions for Conditions, selections and loops​ for my class.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. factorial value
    By joybanerjee39 in forum C Programming
    Replies: 4
    Last Post: 11-14-2011, 11:40 AM
  2. Factorial with while loop
    By GokhanK in forum C Programming
    Replies: 2
    Last Post: 12-26-2010, 12:05 PM
  3. factorial help!
    By hockey1 in forum C Programming
    Replies: 6
    Last Post: 03-13-2009, 04:04 AM
  4. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  5. how to add a factorial ()
    By correlcj in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2002, 02:28 PM

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