By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

Finding maximum value of a data type

P: 22
I was trying the program to calculate the factorial of a number. The program is to identify the value of number at the which the factorial crosses the range of the data type. How should i do this? Please help me for the logic. I want to accommodate all kinds of data types.

Thanks.
Oct 1 '15 #1

✓ answered by weaknessforcats

In C just look in <limits.h>.

In C++ look in <limits>. Learn how to use he numeric_limit class template.

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
In C just look in <limits.h>.

In C++ look in <limits>. Learn how to use he numeric_limit class template.
Oct 1 '15 #2

P: 22
I made the following attempt, but if fails
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<limits.h>
  3. int main(void)
  4. {
  5.    int number=0;
  6.    unsigned short factorial=1;
  7.    printf("Enter the number to calculate factorial\n");
  8.    scanf("%d",&number);
  9.    printf("us= %d\n",sizeof(unsigned short));
  10.    while(number>1)
  11.    {
  12.        factorial *= number;
  13.        if(factorial > USHRT_MAX)
  14.        {
  15.          break;
  16.        }
  17.        number -= 1;
  18.        printf("%d\n",factorial);
  19.    }
  20.    return 0;
  21. }
  22.  
And the output looks something like this
Expand|Select|Wrap|Line Numbers
  1. Enter the number to calculate factorial
  2. 9
  3. us= 2
  4. 9
  5. 72
  6. 504
  7. 3024
  8. 15120
  9. 60480
  10. 50368
  11. 35200
  12. Process returned 0 (0x0)   execution time : 1.874 s
  13. Press any key to continue.
  14.  
After 60480 the value of factorial crosses the range. How to identify this?
Oct 2 '15 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:

Expand|Select|Wrap|Line Numbers
  1. while(number>1)
  2.    {
  3.        factorial *= number;
  4.        if(factorial > USHRT_MAX)
  5.        {
  6.          break;
  7.        }
  8. etc...
doesn't work because factorial has changed before it is tested to see if the max is exceeded.

You have to test before factorial is changed. Therefore, check before you multiply. The room left is USHRT_MAX - factorial. Next check to see how many factorial fit in the room by doing (USHRT_MAX - factorial)/factorial. If the number of factorials left is greater than the number you are going to multiply factorial by, then the new amount will fit:

Expand|Select|Wrap|Line Numbers
  1.     int count = 1;
  2.     while(count <= number)
  3.    {
  4.        if (USHRT_MAX - factorial)/ factorial) > count)
  5.        {
  6.           factorial *= count;
  7.        }
  8.        else
  9.        {
  10.            break;
  11.        }
  12.               ++count;
  13.        printf("%d\n",factorial);
  14.    }
  15.  
Also see that I use the loop counter to do the multiply rather than counting down.
Oct 2 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.