473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Finding maximum value of a data type

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.

3 1843
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
22
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
4
by: Felix Kater | last post by:
Hi, when I use something like int Shift= 3; long Value= 1 << Shift; What is the data type of the const value '1' here? In other terms: What is the possible maximum of 'Shift' here?
6
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using the VScrollBar and set it as follow: m_vScrollBar.Minimum = -19602; m_vScrollBar.Maximum = 0; m_vScrollBar.SmallChange = 1; m_vScrollBar.LargeChange = 1089; m_vScrollBar.Value =...
15
by: timothytoe | last post by:
Situation: I have an array of objects. I want to find the maximum value of a given numeric value that exists in each of the objects. Suppose the array of objects is called "stat" and the numeric...
0
by: Fredrik Lundh | last post by:
maurizio wrote: What kind of file is it? Did you pick numpy because you want to do matrix operations (beyond just finding a maximum value), or was it just the first thing you stumbled upon...
1
by: maurizio | last post by:
thank you for your answer actually i've to do some statistics (maximum,minimum,mean,standard deviation,....) of a file of data in which each column is a particular type of data. (the file is a tab...
1
by: =?Utf-8?B?SGlwSG9wcGVy?= | last post by:
Hi, What's the Minimum and Maximum value for RangeValidator currency type? For example form -99,999,999,999,999 to 99,999,999,999,999? -- HipHopper
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.