Advertisement
M4ritimeSeeker

PASS 10/15 solutions

Oct 15th, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. PROBLEM 1
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. int main(void)
  8. {
  9. srand(time(NULL));
  10.  
  11. /** LOCAL DECLARATIONS GO HERE ***/
  12. int flips = 1000;
  13. int i;
  14. int numHeads = 0;
  15. int numTails = 0;
  16. int HorT = 0; //Random number
  17.  
  18. /** FOR LOOP GOES HERE ***/
  19. for(i = 0; i < flips; i++){
  20. HorT = rand() % 2;
  21.  
  22. if (HorT == 1){
  23. numHeads++;
  24. }
  25. else{
  26. numTails++;
  27. }
  28.  
  29. }
  30.  
  31. printf("Heads = %d\n", numHeads);
  32. printf("Tails = %d\n", numTails);
  33.  
  34. return 0;
  35. }//end main
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. PROBLEM 2
  47.  
  48. /*
  49. * Compose a program using a for loop that will determine the
  50. * factorial of a user supplied number.
  51. * BY: Larry Snedden
  52. */
  53. #include <stdio.h>
  54. int main(void)
  55. {
  56. //Local declarations
  57. int factorial = 1;
  58. int i;
  59. int n = 0;
  60. //Local statements get inputs, process then output
  61. printf("Enter in a number for factorial: ");
  62. scanf("%d", &n);
  63. for(i = 1; i <= n; i++ )
  64. {
  65. factorial = factorial * i;
  66. }
  67. //Output results
  68. printf("Factorial of %d! = %d\n", n, factorial);
  69. return 0;
  70. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement