Last active
November 1, 2021 09:35
-
-
Save trashvin/d12e1885ec8eec7acba9da6d8d66a3be to your computer and use it in GitHub Desktop.
sample c++ code for checkingif double numbers are divisible to a number using modf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <math.h> | |
using namespace std; | |
bool isDivisible(double number, double divisor); | |
int main() | |
{ | |
// generate 20 random numbers and checkif div by 10 | |
for (int i = 0; i < 20; i++) | |
{ | |
double number = rand() % 1000; | |
if (isDivisible(number, 10)) | |
{ | |
cout << "the number " << number << " is divisible by 10" << endl; | |
} | |
else | |
{ | |
cout << "the number " << number << " is NOT divisible by 10" << endl; | |
} | |
} | |
return 0; | |
} | |
bool isDivisible(double number, double divisor) | |
{ | |
double floatPart, integerPart = 0; | |
double quotient = number / divisor; | |
if (modf(quotient, &integerPart) == 0) | |
return true; | |
else | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
initial