Solved

Working out factorial positive integer

Posted on 2003-11-20
12
553 Views
Last Modified: 2010-05-18
i need to work out the factorial of a positive integer. ie the number multiplied by all the integers smaller than it down to 1. eg "5" = 5*4*3*2*1 = 120.
i also need to incorporate a test harness.
the code i have written works out teh factors of any given number but i am not sure off how to work out the final answer. ie. how to multiply them together.

#include<stdio.h>

void main (void){

      int num,x;
      void factorial(int);

      printf("Please enter an integer: ");
      scanf("%d", &num);
      fflush(stdin);



      for(x=1; x<num; x++)
            factorial (x);
}

void factorial (int x){
      printf("x = %d\n", x);


help would b much appreciated. cheers.
0
Comment
Question by:pete420
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9789717
#include<stdio.h>

void main (void){

     int num,x;
     void factorial(int);

     printf("Please enter an integer: ");
     scanf("%d", &num);
     fflush(stdin);



     for(x=1; x<num; x++)    /* A */
          factorial (x);    
}

void factorial (int x){
     printf("x = %d\n", x);

At A it looks like you are going to be calculating separate factorials for each x from 1 to num. So for 1 you get 1, for 2 you get 2, for 3 you get 6 for 4 you get 24 etc. Is that what you want?

The factorial function itself needs to multiply all the numbers from itself down to 1. So a loop is indicated:

for(i=x; i>=1; --i)
{   ...
}

So inside this loop you should have a variable that you successively multiply by i to get the factorial of x.
0
 

Author Comment

by:pete420
ID: 9789780
yes your examples are correct of what i want. .

are u suggesting i remove my loop at A? the factorial i am tryin to work out is of the variable 'num'.

updated code:
#include<stdio.h>

void main (void){

      int num,x,i;
      void factorial(int);

      printf("Please enter an integer: ");
      scanf("%d", &num);
      fflush(stdin);



      for(x=1; x<num; x++)
            for(i=x; i>=1; --i){  
                  num = num *  x;
            }
            factorial (x);
}

void factorial (int x){
      printf("x = %d\n", x);

}
0
 
LVL 4

Accepted Solution

by:
dhyanesh earned 250 total points
ID: 9789786
Hi

Your factorial function should be somewhat like:

void factorial(int x)
{
     int i,fact = 1;
     for(i=1;i<=x;i++)
         fact = fact * i;
     printf("fact = %d\n", fact);
}

Thus this will calculate factorial of a number 'x' that is passed to this function and also print the factorial.

Dhyanesh
0
 

Author Comment

by:pete420
ID: 9789809
excellent! many thanks!:D
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9789833
Hi

If you are finding factorial of 'num' only then the loop should be in function as I have put in my code.

Also then you only need to call the function ONCE and not in a for loop.

In what you have just posted is not right. Your variable num is continuously increasing resulting in outer for loop running almost infinitely.

Dhyanesh
0
 

Author Comment

by:pete420
ID: 9789850
this code below works spot on. im not sure what u mean in the above post. can u show me which loop u were refering to. thanks.

#include<stdio.h>

void main (void){

      int num,x,i;
      void factorial(int, int);

      printf("Please enter an integer: ");
      scanf("%d", &num);
      fflush(stdin);

      for(x=1; x<num; x++)
            for(i=x; i>=1; --i){  
            }
            factorial (x, num);
}


      void factorial(int x, int num)
{
     int i,fact = 1;
     for(i=1;i<=x;i++)
         fact = fact * i;
     printf("Factorial of %d = %d\n", num, fact);
}
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 16

Expert Comment

by:imladris
ID: 9789933
The for loops here:

for(x=1; x<num; x++)
          for(i=x; i>=1; --i){  
          }
          factorial (x, num);

Are completely redundant. They aren't doing anything useful. x increments from 1 to num, and for each of those iterations, i decrements from x to 1. So x and i go up and down a bunch. After that is all done factorial is called which then calculates the factorial for num.

Also, the factorial function should only get num passed in. And num should be used as the maximum of the loop. The x variable isn't really contributing anything to the factorial function either.
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9789938
Hi

--->for(x=1; x<num; x++)              //These two loops are doing nothing
 --->       for(i=x; i>=1; --i){  
         }
         factorial (x, num);

If you are finding factorial of only ONE number i.e. then only following is sufficient:


#include<stdio.h>

void main (void){

    int num,x,i;
    void factorial(int, int);

    printf("Please enter an integer: ");
    scanf("%d", &num);
    fflush(stdin);

             factorial (x, num);
}


    void factorial(int x, int num)
{
    int i,fact = 1;
    for(i=1;i<=x;i++)
        fact = fact * i;
    printf("Factorial of %d = %d\n", num, fact);
}
0
 

Author Comment

by:pete420
ID: 9789951
whenever i run the code u just posted and enter the integer as 5. the answer i receive is 1?? the correct answer would b 120.
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9790006
Hi

One more error. Sorry for that. In factorial you only need to pass ONE variable i.e num. 'x' is redundant

#include<stdio.h>

void main (void){

   int num,x,i;
   void factorial(int, int);

   printf("Please enter an integer: ");
   scanf("%d", &num);
   fflush(stdin);

   factorial (num);
}


   void factorial(int x)
{
   int i,fact = 1;
   for(i=1;i<=x;i++)
       fact = fact * i;
   printf("Factorial of %d = %d\n", x, fact);
}  

This would work just fine.

Dhyanesh
0
 

Author Comment

by:pete420
ID: 9790019
MANY thanks! i got anoter c question which im stuck on. will open another thread so that sum more points are available for it:)
0
 

Expert Comment

by:harishg2
ID: 9816548
void facto(int x)
{
     int k,fact = 1;
     for(k=x;k<=x;k--)
         fact = fact * k;
     printf("fact = %d\n", fact);
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now