Advertisement
Guest User

question 3

a guest
Jan 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. /*
  2. Lab exam winter semester 2016/2017.
  3. Lecture: IE-B1-SO1 (Software construction 1)
  4. Student: Daniel Dedoukh
  5. */
  6.  
  7. #define _CRT_SECURE_NO_DEPRECATE
  8. #include <stdio.h>
  9. #include <math.h>
  10. #define DIM 2
  11.  
  12. // Define boolean
  13. typedef enum
  14. {
  15. FALSE = 0, // no need to set value to 0, but it's better for understanding
  16. TRUE = 1
  17. }boolean;
  18.  
  19. /* Function prototypes */
  20. int isEven(int);
  21. boolean isPrimeNumber(int);
  22.  
  23. int main(void)
  24. {
  25. int num;
  26. int count = 0;
  27.  
  28. // Get user input
  29. printf("Enter maximum number to test ( positive integer number ) : ");
  30.  
  31. while ((scanf("%d", &num) != 1) || (num < 1))
  32. {
  33. printf("Invalid input, must be positive integer. Retry: ");
  34. while (getchar() != '\n')
  35. continue;
  36. }
  37.  
  38. while (getchar() != '\n')
  39. continue;
  40.  
  41. // Print prime numbers
  42. printf("Prime numbers in [1, %d]: \n", num);
  43.  
  44. for (int i = 0; i <= num; i++)
  45. {
  46. if (isPrimeNumber(i) == TRUE)
  47. {
  48. printf("%3d ", i);
  49. count++;
  50.  
  51. if ((count % 10) == 0)
  52. putchar('\n');
  53. }
  54. }
  55.  
  56. printf("\nThere are %d prime numbers in [1, %d]: \n", count, num);
  57.  
  58. getchar();
  59. return 0;
  60. }
  61.  
  62. int isEven(int a)
  63. {
  64. /* Test even numbers */
  65. if ((a % 2) == 0)
  66. return TRUE; // if number is even return TRUE
  67.  
  68. else if ((a % 2) != 0)
  69. return FALSE; // if number is not even return FALSE
  70.  
  71. return 0;
  72. }
  73.  
  74. boolean isPrimeNumber(int a)
  75. {
  76. if (a <= 1)
  77. return FALSE;
  78.  
  79. /* Test even numbers */
  80. if ((isEven(a) == TRUE) && (a >= 4))
  81. return FALSE;
  82.  
  83. /* Test odd numbers */
  84. for (int i = 3; i < a / 2; i += 2)
  85. {
  86. if ((a % i) == 0)
  87. return FALSE;
  88. }
  89. return TRUE;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement