Im trying to create a random number generator and judging the random integers odd or even using a srand call and boolean but i cant figured out how to get it to distinguish properly between whats odd and whats even.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int i;
int num1;
int num2;
bool isOdd (int num1, int num2);
int main(void)
{
srand(time(NULL));
for (i=1; i <= 10; ++i) {
num1 = rand() % 10 + 1;
num2 = rand() % 10 + 1;
printf("The two random numbers are %u and %u\n", num1, num2);
bool valueIsOdd = isOdd(num1, num2);
if (valueIsOdd) {
printf("one of these numbers, %u and %u, isOdd.\n\n", num1, num2);
}
else {
printf("Both of these numbers, %u and %u, are even.\n\n", num1, num2);
}
}
}
bool isOdd(int num1, int num2)
{
if (num1 % 2 != 0) {
return true;
}
else {
return false;
}
}
Aucun commentaire:
Enregistrer un commentaire