Thread: while loop working out inverse of a factorial

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    while loop working out inverse of a factorial

    hi all i have the following code:
    Code:
         int x, factorial;
        double  value, inverse_factorial;
    
        printf("Enter the smallest value of 1/n!: ");
        scanf(" %lf", &value);
    
        printf("value = %lf\n", value);
        x = 2;
        factorial = 1; //set to 1!
        inverse_factorial = 1;
        while (inverse_factorial > value)
        {
            factorial *= x; // 1!*2=2! 2!*3=3!......8!*9=9!
            x++; //incriment multiplier
            inverse_factorial = 1/factorial;
        }
        printf("x = %d\n", x);
    
        return 0;
    the only value of x i ever get is 3 no matter how small i set value and i cant for the life of me see why

    any suggestions greatly appreciated
    coop

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    When both operands are integers, the result is an integer. If you want a double, either (or both) of them need to be double. Here, just use "1.0" instead of "1".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks.... annoying thing is it was ..........ing because i had scanf("%f", &value); i was getting a warning about putting a float into a double but answer = 1/factorial you get bugger all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2016, 08:49 AM
  2. Working out a factorial using arrays
    By Elessar in forum C Programming
    Replies: 4
    Last Post: 11-27-2013, 12:31 PM
  3. Factorial using a while loop
    By sourpatchkid in forum C Programming
    Replies: 39
    Last Post: 05-14-2013, 12:25 PM
  4. Factorial with while loop
    By GokhanK in forum C Programming
    Replies: 2
    Last Post: 12-26-2010, 01:05 PM
  5. Factorial Using recursion not working
    By rrahulvverma in forum C Programming
    Replies: 4
    Last Post: 10-08-2010, 09:00 AM

Tags for this Thread