Factorial in C language (with loop)

Closed
Anonymous user -  
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   -
Hello,

I need to develop a program that will calculate the factorial:
- using a loop (while or for),
- through recursive function calls (n! = n x (n-1)!)
The user will enter the integer, and the program will return the formatted result on the screen.

We haven't covered loops in class yet, so could someone help me write these programs?

Next, we are asked to explore the role that choosing to work with long integers (long int), short integers (short int), or regular integers plays: what is the maximum value of n possible in each case? Use the C utility sizeof() to determine the byte size of the declared types.

We haven't covered long and short integers and sizeof() yet, so could someone explain to me what needs to be done?

Then, we are asked to perform the same calculation using the multiplication of real numbers (float) and double precision (double). Is there an improvement in your results compared to the calculation done with integers? What is the largest integer N that can be processed in each case?

We haven't covered double precision yet, so could someone explain to me what needs to be done?

Thank you very much in advance for your answers.

3 answers

sebastien61 Posted messages 495 Registration date   Status Member Last intervention   59
 
Hello,

for the loop part:

 int n; // the integer for which you want to calculate the factorial int result = 1; // the result of the calculation int i = n; // a temporary index while (i>1) { result = result * i; i--; // equivalent to i = i-1; } // here result is equal to n! 


For the recursive method, it's easier:

 int facto(int n) { if (n==0) // we test the simple case: case when n=0 return 1; else { return n * facto(n-1); // otherwise we return n * facto(n-1) } } 


If you have any questions, don't hesitate!
5
artragis Posted messages 510 Status Member 146
 
A loop allows you to repeat an action (always the same) as long as a condition is true.
For example
while i<1 write too small read i end while 

The for loop allows you to perform the same action multiple times a certain number of times
c=1 for i=1 to i=10 in steps of 1 do c+1 end for

The loop added 1 to C each time as long as 1 was less than 10.

A factorial is multiplying the result by the next integer. For example !5 = 1*2*3*4*5
From there you can create your loop. The shortest is a for loop; however, if you didn't understand it, a while loop works just fine.

Now, a signed integer is 8 bits, so it ranges from -128 to 127.
A long integer is signed 16 bits; therefore, you need to look at powers of 2: 2^0 + 2^1 + 2^2 + ... + 2^16, and you will have the maximum number you can take, knowing that there are as many negatives as positives with an exception of 1.

For single precision, it takes a certain number of decimals; double precision takes double that (do a web search, you'll find it easily). In your exercise, you are working with integers... it's up to you to draw your conclusion.
3
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
 
Hello,

We haven't seen loops yet, so can someone help me write these programs?

I don't think the teacher gave you exercises with loops since we haven't done them in class already.

I also don't see much effort on your part to show us what you've done.
What I see is you asking us to do the exercises for you.
The answer is there Ask for help with your exercises on CCM
Out of respect for those who have already posted a solution, I will not delete the message.
That will be for the future if you continue in this direction.
You have a riddle here http://www.commentcamarche.net/forum/affich 11290011 riddle in C language?#1

I am closing this.
--
106485010510997108
2