Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,386 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,221 people online right now. Registration is fast and FREE... Join Now!




calculate and list factorial 1-50

 
Reply to this topicStart new topic

calculate and list factorial 1-50, C++ language

novicego
post 21 Aug, 2008 - 09:48 PM
Post #1


New D.I.C Head

*
Joined: 21 Aug, 2008
Posts: 3

I am supposed to write a program to list the numbers from 1-50 and their factorials side by side. So far I have the following, but I am obviously missing something. Can anybody help please?

CODE

#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

int main()
{
    int num, factorial;
    factorial = 1;
    num = 1;

    /* Calculate factorial 1-50*/
    for(num=1; num<51; num=num+1)
    {
      while(num>=1)
      {
        fact = fact*num;        
        num = num-1;
      }
    }

    /* Display number and factorial */
    printf("%d      %d", num, factorial);

    return 0;
}


Thanks!
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 21 Aug, 2008 - 09:55 PM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


See this post. See the problem is, 50! will not fit into an int. You could use a double but that would probably not be very accurate. So to get up to 50! you will need an arbitrary precision arithmetic library (a bignum library). There are a few snippets on this sort of think in the snippets section.
User is offlineProfile CardPM

Go to the top of the page

novicego
post 21 Aug, 2008 - 10:16 PM
Post #3


New D.I.C Head

*
Joined: 21 Aug, 2008
Posts: 3

QUOTE(novicego @ 21 Aug, 2008 - 10:48 PM) *

I am supposed to write a program to list the numbers from 1-10 and their factorials side by side. So far I have the following, but I am obviously missing something. Can anybody help please?

CODE

#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

int main()
{
    int num, factorial;
    factorial = 1;
    num = 1;

    /* Calculate factorial 1-10*/
    for(num=1; num<11; num=num+1)
    {
      while(num>=1)
      {
        factorial = factorial*num;        
        num = num-1;
          /* Display number and factorial */
         printf("%d            %d", num, factorial);
      }
    }

    

    return 0;
}


My first posted code was wrong. This is the one I have now. I run it, and I get the numbers 0 and 10 and 1 in columns non stop.

It is from 1 to 10.

Thanks!

User is offlineProfile CardPM

Go to the top of the page

Mallstrop
post 23 Aug, 2008 - 02:38 AM
Post #4


New D.I.C Head

*
Joined: 19 Jun, 2008
Posts: 32



Thanked 1 times
My Contributions


CODE

for(num=1; num<11; num=num+1)
    {
      while(num>=1)
      {
        factorial = factorial*num;        
        num = num-1;
          /* Display number and factorial */
         printf("%d            %d", num, factorial);
      }
    }


The inner loop is changing the loop variable used in the outer loop so it's not running as you expected it to.

Just a quick hint on a simple solution.

Each factorial number is the previous solution multiplied by the current position.
CODE
total = 1;
for i is 1 to 10
     multiply total by i
     print ( i  and  total)
done

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:54AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month