Skip to content

Instantly share code, notes, and snippets.

@trashvin
Last active November 1, 2021 09:35
Show Gist options
  • Save trashvin/d12e1885ec8eec7acba9da6d8d66a3be to your computer and use it in GitHub Desktop.
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
#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;
}
@trashvin
Copy link
Author

trashvin commented Nov 1, 2021

initial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment