Advertisement
programusy

Untitled

Feb 9th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. bool is_prime(int num) {
  9. if (num <= 1) return false;
  10. for (int i = 2; i <= sqrt(num); i++) {
  11. if (num % i == 0) return false;
  12. }
  13. return true;
  14. }
  15.  
  16. int main() {
  17. int array[50];
  18. int count = 0;
  19. int min_prime = INT_MAX;
  20. int max_prime = INT_MIN;
  21. int first_digit, last_digit;
  22. srand(time(0));
  23.  
  24. for (int i = 0; i < 50; i++) {
  25. array[i] = rand() % 401 + 100;
  26. }
  27.  
  28. cout << "Liczby pierwsze w tablicy:" << endl;
  29. for (int i = 0; i < 50; i++) {
  30. if (is_prime(array[i])) {
  31. cout << array[i] << " ";
  32. count++;
  33. if (array[i] < min_prime) min_prime = array[i];
  34. if (array[i] > max_prime) max_prime = array[i];
  35. }
  36. }
  37.  
  38. cout << endl << "Ilosc liczb pierwszych w tablicy: " << count << endl;
  39. cout << "Najmniejsza liczba pierwsza w tablicy: " << min_prime << endl;
  40. cout << "Najwieksza liczba pierwsza w tablicy: " << max_prime << endl;
  41.  
  42. cout << endl << "Liczby, w ktorych pierwsza i ostatnia cyfra sa takie same:" << endl;
  43. int max_num=0;
  44. for (int i = 0; i < 50; i++) {
  45. first_digit = array[i] / 100;
  46. last_digit = array[i] % 10;
  47. if (first_digit == last_digit)
  48. {
  49. cout << array[i] << " ";
  50. if(max_num == 0 || max_num < array[i]) max_num = array[i];
  51.  
  52. }
  53. }
  54. cout <<endl<< "maksymalna liczba z pierwszych i ostatnich takich samych: "<<max_num<<endl;
  55. return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement