473,322 Members | 1,259 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,322 software developers and data experts.

Checking weather a number is whole or not in C.

Hello,

I am trying to figure out how I'm supposed to write a piece of code which tells my whether a number is whole or not when divided by a second number.

I am fairly new to programming, but I do have a lot of experience in web design and scripting so I'm not a total beginner and have a good understanding of the concepts involved.

For example:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5.     int number1 = 20;
  6.     int number2 = 25;
  7.     int divisor    = 2;
  8.  
  9.     if(number1/divisor == #Whole Number#){
  10.     printf("Result 1 is an integer\n");
  11.     }else{
  12.     printf("Result 1 is not an integer\n");
  13.     }
  14.  
  15.     if(number2/divisor == #Whole Number#){
  16.     printf("Result 2 is an integer\n");
  17.     }else{
  18.     printf("Result 2 is not an integer\n");
  19.     }
  20.     return 0;
  21. }
  22.  
This crude mock-up of my code would produce an outcome similar to:
Expand|Select|Wrap|Line Numbers
  1. Result 1 is an integer
  2. Result 2 is not an integer
Thank you in advance if you can help.
Aug 22 '07 #1
3 2151
ilikepython
844 Expert 512MB
Hello,

I am trying to figure out how I'm supposed to write a piece of code which tells my whether a number is whole or not when divided by a second number.

I am fairly new to programming, but I do have a lot of experience in web design and scripting so I'm not a total beginner and have a good understanding of the concepts involved.

For example:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5.     int number1 = 20;
  6.     int number2 = 25;
  7.     int divisor    = 2;
  8.  
  9.     if(number1/divisor == #Whole Number#){
  10.     printf("Result 1 is an integer\n");
  11.     }else{
  12.     printf("Result 1 is not an integer\n");
  13.     }
  14.  
  15.     if(number2/divisor == #Whole Number#){
  16.     printf("Result 2 is an integer\n");
  17.     }else{
  18.     printf("Result 2 is not an integer\n");
  19.     }
  20.     return 0;
  21. }
  22.  
This crude mock-up of my code would produce an outcome similar to:
Expand|Select|Wrap|Line Numbers
  1. Result 1 is an integer
  2. Result 2 is not an integer
Thank you in advance if you can help.
Use the modulo operator (%):
Expand|Select|Wrap|Line Numbers
  1.     int number = 20;
  2.     int divisor = 2;
  3.  
  4.     if (number % divisor == 0)
  5.     {
  6.         printf("number is divisible by 2");
  7.     }
  8.     else
  9.     {
  10.         printf("number is not divisible by 2");
  11.     }
  12.  
a % b gives the remainder when dividing a by b.
Aug 22 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Exactly what do you mean by whole number?? I mean, all integers are whole numbers and all floating point numbers are not.
Aug 22 '07 #3
donbock
2,426 Expert 2GB
In essence, @lujekatahe suggests
Expand|Select|Wrap|Line Numbers
  1. int number = 20;
  2. int divisor = 2;
  3. double ratio = (double)number / (double)divisor;
  4. if (isWhole(ratio))
  5.    print("number is divisible by 2\n");
  6. else
  7.     print("number is not divisible by 2\n");
There are two problems here.
  1. The double value of variable ratio only approximates the true value of the ratio. Thus, a slightly incorrect value may be passed to isWhole, leading to a garbage-in-garbage-out error.
  2. IsWhole determines if its double input value is a whole number by casting the input value to an int (which truncates any fraction), then implicitly converting that back to a double, and finally checking if the converted value is equal to the original input value. However, comparing floating point values for equality (==) or inequality (!=) can have unreliable results. Refer to What Every Computer Scientist Should Know About Floating-Point Arithmetic. In this particular case I think the comparison will work reliably; but you don't want to get in the habit of making these kind of comparisons.

The OP wants to determine if one int is evenly divisible by another int -- not write a function to determine if an arbitrary floating-point value is a whole number. You will get a more reliable result by using integer math as proposed by @ilikepython.

If you choose to write a general purpose function to use the modulo operator then that function should first verify the divisor is not zero in order to avoid a divide-by-zero exception.
Mar 29 '16 #4

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

Similar topics

0
by: Dennis | last post by:
Trying to check if a drive is writable. Can do it by trying to create a directory using DirectoryInfo class then checking err.number but looking for a more elegant way. Tried...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
1
by: Servé La | last post by:
Is there a standard method to test if a string has a double value in it without using an exception? The only method I find is using Double.Parse(str), but this throws an exception which is...
5
by: Jeff | last post by:
I am trying to crete a method that will convert an improper fraction to a mixed number... I am not sure how about how to acomplish this. I know I can get the remainder with the modulus operator...
4
by: Mark D Smith | last post by:
Hi i have to test a form for valid format of international numbers in the format +44.1234567890 i realize that some countries have 3 numbers after the + but so far apart from checking the...
3
by: pocmatos | last post by:
Hi all, I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits + and keep them in an int, however if the number is bigger than int, I should output error....
2
sonic
by: sonic | last post by:
I am fairly new to C++. I was wondering how would one go about checking to see if a value entered by the user was negative or positive. Is this something you might use modulo(%) math for? ...
1
by: eadavid | last post by:
HI, I am a beginner to perl and I need to write perl code for checking a good die in the wafermap. Here is the wafermap look like in file ....XXXX111111111X1111XXXX.......
5
by: kouisawang | last post by:
I have written a function to check if the number is fraction in either X/X or X X/X format where X is double. Here is the code. bool IsFraction(const CComBSTR bucketSize, double &result) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.