Xavistian

Ejercicio 2 semana 1

Aug 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. // ConsoleApplication1.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <conio.h>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace System;
  8.  
  9. bool Es_Primo(int x)
  10. {
  11.     int cont = 0;
  12.     for (int pdiv = 1; pdiv <= x; pdiv++)
  13.     {
  14.         if (x%pdiv == 0)
  15.             cont++;
  16.     }
  17.     if (cont == 2)
  18.         return true; else return false;
  19. }
  20. int main()
  21. {
  22.     int n, npa=0, npr=0;
  23.     Random x;
  24.     do {
  25.         cout << "Ingrese un numero positivo. " << endl;
  26.         cin >> n;
  27.     } while (n <= 0);
  28.     int *a = new int[n];
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         a[i] = x.Next(10, 10001);
  32.         if (a[i] % 2 == 0)npa++;
  33.         if (Es_Primo(a[i]) == true)npr++;
  34.     }
  35.     int *APAR = new int[npa];
  36.     int j = 0;
  37.     for (int i = 0; i < n; i++)
  38.     {
  39.         if (a[i] % 2 == 0)
  40.         {
  41.             APAR[j] = a[i];
  42.             j++;
  43.         }
  44.     }
  45.     j = 0;
  46.     int *APRIMO = new int[npr];
  47.     for (int i = 0; i < n; i++)
  48.     {
  49.         if (Es_Primo(a[i]) == true)
  50.         {
  51.             APRIMO[j] = a[i];
  52.             j++;
  53.         }
  54.     }
  55.     for (int i = 0; i < npr; i++)
  56.         cout << APRIMO[i] << " ";
  57.     _getch();
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment