You can generate a random number in C using the following co

You can generate a random number in C using the following code: int myRandomNumber; srand(time(NULL)); // seed the random number generator myRandomNumber = rand(); // each time you call this function there will be a different random number. Generate a random number, and output it. Use if statements to determine if the number is odd or even, and output a message to that effect. Similarly, output if the number is divisible by 3, and if it is divisible by 10. Use the % operator to achieve this. Run the program, and print the results several times using different numbers to be sure that it’s working. Hint: In C you can not declare variables after you write executable code. For example if you call the srand function, and then declare some more variables after that call, the program will not compile. The compiler messages are not very helpful. Put all the variable declarations at the top of the function.

Solution

#include <stdio.h>
#include <stdlib.h>

int main()
{
int myRandomNumber;
srand(time(NULL));
// seed the random number generator
myRandomNumber = rand();
if(myRandomNumber % 2 == 0){
printf(\"Random number %d is even number\ \",myRandomNumber);
}
else{
printf(\"Random number %d is odd number\ \",myRandomNumber);
}
  
  
if(myRandomNumber % 3 == 0){
printf(\"Random number %d is divisible by 3\ \",myRandomNumber);
}
else{
printf(\"Random number %d is not divisible by 3\ \",myRandomNumber);
}
  
if(myRandomNumber % 10 == 0){
printf(\"Random number %d is divisible by 10\ \",myRandomNumber);
}
else{
printf(\"Random number %d is not divisible by 10\ \",myRandomNumber);
}
return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                  

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 992903991 is odd number                                                                                                                                                                                                                                    

Random number 992903991 is divisible by 3                                                                                                                                                                                                                                

Random number 992903991 is not divisible by 10                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 692479352 is even number                                                                                                                                                                                                                                   

Random number 692479352 is not divisible by 3                                                                                                                                                                                                                            

Random number 692479352 is not divisible by 10                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1459191701 is odd number                                                                                                                                                                                                                                   

Random number 1459191701 is not divisible by 3                                                                                                                                                                                                                           

Random number 1459191701 is not divisible by 10                                                                                                                                                                                                                          

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1165304989 is odd number                                                                                                                                                                                                                                   

Random number 1165304989 is not divisible by 3                                                                                                                                                                                                                           

Random number 1165304989 is not divisible by 10                                                                                                                                                                                                                          

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1165304989 is odd number                                                                                                                                                                                                                                   

Random number 1165304989 is not divisible by 3                                                                                                                                                                                                                           

Random number 1165304989 is not divisible by 10                                                                                                                                                                                                                          

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 850567710 is even number                                                                                                                                                                                                                                   

Random number 850567710 is divisible by 3                                                                                                                                                                                                                                

Random number 850567710 is divisible by 10                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1624618623 is odd number                                                                                                                                                                                                                                   

Random number 1624618623 is divisible by 3                                                                                                                                                                                                                               

Random number 1624618623 is not divisible by 10

You can generate a random number in C using the following code: int myRandomNumber; srand(time(NULL)); // seed the random number generator myRandomNumber = rand
You can generate a random number in C using the following code: int myRandomNumber; srand(time(NULL)); // seed the random number generator myRandomNumber = rand
You can generate a random number in C using the following code: int myRandomNumber; srand(time(NULL)); // seed the random number generator myRandomNumber = rand

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site