Loop or Loops in "C" 
Author Message
 Loop or Loops in "C"

Quote:

> Just having fun with VC++ here, man it's fun. I have a problem that I'm
> trying to solve. It is possible to do it without a loop, but I want to do it
> with a loop or loops, so there is less code.

> Here it is:
> The factorial of an integer "n", written "n!", is the product of the
> consecutive integers 1 through "n". For example, 5 factorial is calculated
> as:

> 5! = 5 * 4 * 3 * 2 * 1= 120

> I'm trying to figure out how to use a loop or loops to do this. So they will
> generate and print a table of the first 10 factorials.  I don't know if I'm
> looking at this too hard and it's very easy or it may be hard to do with
> loops.Not to sure here..I am writing this in "C" though.

You really should just get a book on the language.  This newsgroup
exists to help you after you've exhausted the most basic resources -
your brain, a book, a friend, and the FAQ.

This problem isn't really all that difficult.  Try writing some
pseudo-code first to see if you can figure out the principle behind the
calculations, and if you get that right then translate it into C code.
For example, my pseudo-code for calculating a factorial would look like:

    result = 1

    for i = 1 to number
        result = result * i

(This is a very simple and incomplete definition of a factorial, but
there's no need to make it more complicated than you need.)

Try posting some C code, even if it doesn't work.  We're more inclined
to help if you've shown a bit of effort, even if you didn't get any
where.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Wed, 28 Feb 2001 03:00:00 GMT  
 Loop or Loops in "C"

Quote:



>>consecutive integers 1 through "n". For example, 5 factorial is calculated
>>as:

>>5! = 5 * 4 * 3 * 2 * 1= 120

>Usually, the multiplication by 1 is omitted. Also, the factorial of
>0 is taken to be 1 so that certain combinatorial formulae work
>correctly. E.g. the number of ways to choose zero items from a set
>of five or five items from a set of five is equal to

>         5!
>      ------
>      (5-0)!0!

>where the 0! is taken to be 1, so the whole thing evaluates to 1.

Or more simply there is 1 way to permute zero items. Or look at it this way

N! * (N+1) = (N+1)!

N! = (N+1)! / (N+1)       Divide both sides by N+1

0! = 1! / 1               Set N=0

0! = 1

--
-----------------------------------------


-----------------------------------------



Wed, 28 Feb 2001 03:00:00 GMT  
 Loop or Loops in "C"

   back on Sat, 12 Sep 1998 17:35:33 -0400, John Kugelman proclaimed ...
   >
   <snipage>
   >
   >    result = 1
   >
   >    for i = 1 to number
   >        result = result * i
   >
   >(This is a very simple and incomplete definition of a factorial, but
   >there's no need to make it more complicated than you need.)
   >

   int factor(int k) {
   int v=1;
       if (k>1)
           v=k * (factor(k-1));
       return (v);
   }

int
factor (int k)
{
  int{*filter*}= 1;
  while (k > 1)
   {*filter*}*= k--;
  return cum;

Quote:
}

Simpler to read, simpler to understand.  Recursion for factorials is
just stupid IMarrogantO.

FWIW, sometimes a better method is to just keep a table of
factorials--after all, 13! > 2^32, so there often aren't very many of
interest.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Wed, 28 Feb 2001 03:00:00 GMT  
 Loop or Loops in "C"
Oh, something like this:

long factorial( int in )
{
    int            count_down;
    long           value = 1;
    for (count_down = in; count_down != 0; count_down--)
        value *= count_down;
    return value;

Quote:
}

Is that what you meant?

--
-----
-Meric

-----



Wed, 28 Feb 2001 03:00:00 GMT  
 Loop or Loops in "C"
back on Sat, 12 Sep 1998 17:35:33 -0400, John Kugelman proclaimed ...

Quote:

<snipage>

>    result = 1

>    for i = 1 to number
>        result = result * i

>(This is a very simple and incomplete definition of a factorial, but
>there's no need to make it more complicated than you need.)

#include <stdio.h>

int factor(int k) {
int v=1;
    if (k>1)
        v=k * (factor(k-1));
    return (v);

Quote:
}

int main() {
    printf("%d\n", factor(6));
    return 0;

Quote:
}

--


Calcasieu Lumber Co.                             Austin TX
- Will sysadmin for food


Thu, 01 Mar 2001 03:00:00 GMT  
 Loop or Loops in "C"

Quote:



>> Oh, something like this:

>> long factorial( int in )
>> {
>>     int            count_down;
>>     long           value = 1;
>>     for (count_down = in; count_down != 0; count_down--)
>>         value *= count_down;
>>     return value;
>> }

>> Is that what you meant?

>> --
>> -----
>> -Meric

>> -----

>since C passes by value, you don't need the count_down variable.

True, however it does no harm. Some people consider not modifying the
values of parameters to be good style. If nothing else it helps if you
discover that you need the original parameter value later on in the
function body. The compilers I've tested don't generate worse code for
using the extra variable.

--
-----------------------------------------


-----------------------------------------



Thu, 01 Mar 2001 03:00:00 GMT  
 Loop or Loops in "C"

Quote:
> The factorial of an integer "n", written "n!", is the product of the
> consecutive integers 1 through "n". For example, 5 factorial is calculated
> as:

> 5! = 5 * 4 * 3 * 2 * 1= 120

> I'm trying to figure out how to use a loop or loops to do this. So they will
> generate and print a table of the first 10 factorials.  I don't know if I'm
> looking at this too hard and it's very easy or it may be hard to do with
> loops.Not to sure here..I am writing this in "C" though.

#include <stdio.h>

double factorial(int n)
{
        double f;
        f=n
        while(--n>1)
                f*=(double)n;

Quote:
}

void main()
{
        int i;
        for(i=1;i<=10;i++)
                printf("%lf\n",factorial(i));
Quote:
}



Sun, 04 Mar 2001 03:00:00 GMT  
 Loop or Loops in "C"
On Wed, 16 Sep 1998 15:17:05 +0300, Alexnader Dymerets

Quote:

>> The factorial of an integer "n", written "n!", is the product of the
>> consecutive integers 1 through "n". For example, 5 factorial is calculated
>> as:

>> 5! = 5 * 4 * 3 * 2 * 1= 120

>> I'm trying to figure out how to use a loop or loops to do this. So they will
>> generate and print a table of the first 10 factorials.  I don't know if I'm
>> looking at this too hard and it's very easy or it may be hard to do with
>> loops.Not to sure here..I am writing this in "C" though.

>#include <stdio.h>

>double factorial(int n)
>{
>    double f;
>    f=n
>    while(--n>1)
>            f*=(double)n;
>}

>void main()
>{
>    int i;
>    for(i=1;i<=10;i++)
>            printf("%lf\n",factorial(i));
>}

Assuming that you meant to put a

        return f;

at the end of factorial(), this has the problem of not giving the
correct result for factorial(0).
--
Michael M Rubenstein



Sun, 04 Mar 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Optimising "conditional" while loop

2. Optimising "conditional" while loop

3. "For" loop problem

4. "for loops" - for newbie

5. HELP with "pretest for() loop"

6. Careful "for" Loops

7. Broken Rule For "For -- Loop"

8. Refresh view in "for" loop

9. "RE: "Re: "RE: Infinite loop ..."

10. For loops into for loops

11. (Newbie) My Loop Isn't Looping - Aargh!

12. SEMA - WHILE loop in a FOR loop !

 

 
Powered by phpBB® Forum Software