Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 10
  4.  
  5. int input_arr(int n, int *a)
  6. {
  7. for (int i = 0; i < n; i++)
  8. {
  9. printf("Input the element of the array: ");
  10. if (scanf("%d", &a[i]) != 1)
  11. return EXIT_FAILURE;
  12. }
  13. return EXIT_SUCCESS;
  14. }
  15.  
  16. int selection(int *main_arr, int *answr_arr, int n)
  17. {
  18. int k = 0;
  19. int flag = 0;
  20.  
  21. for (int i = 0; i < n; i++)
  22. {
  23. if (main_arr[i] > 1)
  24. {
  25. for (int j = 2; j < main_arr[i]; j++)
  26. {
  27. if (main_arr[i] % j == 0)
  28. {
  29. flag = 1;
  30. }
  31. }
  32. if (flag != 1)
  33. {
  34. answr_arr[k] = main_arr[i];
  35. k++;
  36. }
  37. flag = 0;
  38. }
  39. }
  40. if (k == 0)
  41. {
  42. printf("There are no prime numbers in the sequence");
  43. return EXIT_FAILURE;
  44. }
  45. else
  46. {
  47. for (int i = 0; i < k; i++)
  48. printf("%d", answr_arr[i]);
  49. }
  50. return EXIT_SUCCESS;
  51. }
  52.  
  53. int main()
  54. {
  55. int main_arr[MAX];
  56. int answr_arr[MAX];
  57. int n;
  58.  
  59. int rc;
  60.  
  61. printf("Input the number of array elements: ");
  62. rc = scanf("%d", &n);
  63.  
  64. if (rc != 1 || n <= 0 || n > 10)
  65. {
  66. printf("Error");
  67. return EXIT_FAILURE;
  68. }
  69.  
  70. if (input_arr(n, main_arr) == EXIT_FAILURE)
  71. return EXIT_FAILURE;
  72.  
  73. if (selection(main_arr, answr_arr, n) == EXIT_FAILURE)
  74. return EXIT_FAILURE;
  75.  
  76. return EXIT_SUCCESS;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement