-7

I am currently stuck on a problem for my intro to programming class assignment. I have to write a function that calculates the factorial of a number. I know how to do that as you can see below. However The assignment is asking for:

Test you function by calling it from the main. Call the function in a loop, an output the results like this:

Factorials:
1: 1
2: 2
3: 6
4: 24
5: 120
… etc.

The function will stop working when the numbers get too large to fit in your variables. What data type should you use to calculate the most factorials?

I am totally lost at this. I wanted to know if someone could guide me in what to do? I have pasted my code below:

#include <stdio.h>

int main(void){
    int number;

    printf("Enter an integer: ");
    scanf("%i", &number);

    factorial(number);

    return 0;
}


int factorial(int number){
    int fac = 1, count;

    count = number;
    while(count > 1){
        fac = fac * count;
        count = count - 1;
    }

    printf("The factorial: %i\n", fac );

}
dragosht
  • 3,237
  • 2
  • 23
  • 32
  • There is no builtin data type for very large numbers. You need to program your own, ot use a third party library like gmp. – n. m. could be an AI Jul 24 '14 at 04:38
  • Your task is to add a loop to the `main()` function, and change the output format. You already have a loop in your other function, so you know how to write loops. You already have a couple of `printf` statements, so you know how to display output. What, specifically, is the problem you are stuck on? – Greg Hewgill Jul 24 '14 at 04:40
  • How do i create a loop like its asking. If I enter 5 how will do I show from 1 to 5? – user3799479 Jul 24 '14 at 04:41
  • @user3799479 use `bignum` library – Sathish Jul 24 '14 at 04:41
  • Seems to me you can solve the assignment before writing any code by deciding which variable will hold the largest result. – codenheim Jul 24 '14 at 04:41
  • 3
    "I am totally lost at this." -- What is "this"? You wrote "Test you function by calling it from the main. Call the function in a loop, an output the results like this ... The function will stop working when the numbers get too large to fit in your variables. What data type should you use to calculate the most factorials?" -- you haven't told us what part of that you're lost at. – Jim Balter Jul 24 '14 at 04:43
  • By this i mean how do I show the factorial of each number. Like I show in the example given to me from the assignment: Factorials: 1: 1 2: 2 3: 6 4: 24 5: 120 I am guess this is the output I get if I enter the number 5 and it shows me from 1 to 5. I don't know how to do this. – user3799479 Jul 24 '14 at 04:47
  • What part of that don't you understand how to do? As Greg Hewgill said, you already know how to write loops. And you already know how to print things. Just do it ... write a loop and print what it says to print. At least post some sort of attempt to do that. – Jim Balter Jul 24 '14 at 04:49
  • "this is the output I get if I enter the number 5 " -- it doesn't say anything about entering the number 5. It says **etc.** and talks about the values getting too big. Clearly they want you to print more than 5. – Jim Balter Jul 24 '14 at 04:52
  • That was just an example I was giving Jim. I wanted to know how do I show the factorials of the numbers below 5 if the value entered was 5 like in the example: Factorials: 1: 1 2: 2 3: 6 4: 24 5: 120 – user3799479 Jul 24 '14 at 04:56
  • Again: you already know how to write a loop and how to print. Just write some code. – Jim Balter Jul 24 '14 at 04:59
  • Thats the part i am having trouble with Jim. I don't understand how to write the code in which it would print the numbers below the value i entered. – user3799479 Jul 24 '14 at 05:02
  • 1
    Take a stab at it. You haven't even tried. Here at SO we expect people to *try* to solve problems first before asking questions. If you wrote that factorial function then you already know how to do it. Did you write it, or did you copy it from someone else without understanding it? – Jim Balter Jul 24 '14 at 05:03
  • I am some idea jim and its not that i haven't tried. I have to create some type of loop. – user3799479 Jul 24 '14 at 05:05
  • 2
    You haven't posted any code showing that you tried. Good luck ... I'm done here. – Jim Balter Jul 24 '14 at 05:07

2 Answers2

1

You should use uintmax_t. It is defined in stdint.h.

It is an unsigned integer type with the maximum width supported for the platform.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You can use unsigned long long type for the larger numbers but upto some limit only.

prince
  • 1,129
  • 1
  • 15
  • 38
  • when i use unsigned long I am getting weird numbers like this: Enter an interger : 32 The Factorial: -2147483648 – user3799479 Jul 24 '14 at 04:51
  • @user3799479 Try reading your assignment: "The function will stop working when the numbers get too large to fit in your variables. " – Jim Balter Jul 24 '14 at 04:53
  • Prince how would I show the factorials of the pervious numbers. Lets say I enter 6 and I want to show all the values until 6 like this: factorial: 1: 1 2: 2 3: 6 4: 24 5: 120 6: 720 I don't know how to do this and this is the part i am stuck at completely. – user3799479 Jul 24 '14 at 05:04
  • 2
    See the OP's comments under the question ... s/he hasn't tried at all. I doubt that the OP even wrote that factorial function, because whoever did write it certainly knows how to iterate over consecutive integers. – Jim Balter Jul 24 '14 at 05:15
  • I did write it. Don't jump to ostensible conclusions without any kind of proof. This is my first time programming and learning about loops that is why I am having a hard time with this. I am actually sitting here trying to figure this out. Like you said you want me to show I have tried something and that is what I am currently doing. – user3799479 Jul 24 '14 at 05:20
  • You can check this question and get the answer http://stackoverflow.com/questions/12892360/getting-all-prime-numbers-between-2-and-100-in-c – prince Jul 24 '14 at 05:27
  • You have to use the correct printing format in printf. If you're using gcc or clang on 64-bit platforms you can also use `__int128_t`, that'll provide you some more range to breathe – phuclv Jul 24 '14 at 05:34
  • Thank you for your help prince. That example helped me alot! – user3799479 Jul 24 '14 at 05:46
  • 1
    @user3799479 One doesn't need proof to express a doubt ... mine was well justified. – Jim Balter Jul 24 '14 at 06:40