Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to take mod of double data type to check if its even or not.

What I have tried:

sry for that...
#include <stdio.h>

 
int main()
{
     long n;
    scanf("%d",&n);
    while(n--){
        double g;
        scanf("%d",&g);
        if(((int)g)%2==0)
        printf("YES\n");
        else
        printf("NO\n");
        
    }
}

Constraints
1<=n<=100000
1<=g<=10^100

here n is the no. of test case
and g is the no. to check whether its even or not.
sirr its not working.
Posted
Updated 1-Sep-17 4:12am
v3
Comments
jeron1 1-Sep-17 10:24am    
Is the constraint for g correct, 10 to the 100th power?
CPallini 1-Sep-17 13:30pm    
As jeron1 noticed, you cannot hold such big numbers in a double variable.
jatinp510 2-Sep-17 0:27am    
which data type i have to use to hold value for 10^100.
jeron1 2-Sep-17 11:53am    
You would need something like GitHub - sercantutar/infint: Arbitrary-Precision Integer Arithmetic[^] (I've not used it personally, so I don't know how well it works). You could Google 'c++ bigint' and see what comes up.

Convert it to an int by casting, and use the integer modulus operator "%":
C++
if (((int) myDouble) % 2 == 0)
   {
   // It's even.
   ...


And BTW: do not swear on this site again: it was unnecessary, unprofessional, and unwanted. Do it again and you will be treated as a troll, and banned from the site.
 
Share this answer
 
Comments
jatinp510 1-Sep-17 9:07am    
sry for that...

#include <stdio.h>


int main()
{
long n;
scanf("%d",&n);
while(n--){
double g;
scanf("%d",&g);
if(((int)g)%2==0)
printf("YES\n");
else
printf("NO\n");

}
}
Constraints
1<=n<=100000
1<=g<=10^100

here n is the no. of test case
and g is the no. to check whether its even or not.
sirr its not working.
Jochen Arndt 1-Sep-17 9:48am    
The error is at
scanf("%d",&g);

You are reading an int and store it at a memory location for a double.
Use
scanf("%lf",&g);

instead to tell scanf that it should convert a double.
jatinp510 1-Sep-17 10:08am    
ITS STILL NOT WORKING
OriginalGriff 1-Sep-17 10:09am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
jatinp510 1-Sep-17 10:11am    
Okay. :)
Doubles aren't even or odd (they are with near zero probability). You have to convert them to integers. The way you do it may bias the result: you may choose, for instance to truncate the double value (as Griff suggested) or to round it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900