programowanie c++

Zadanie 5
Napisz program, który wylosuje 20 liczb z przedziału <1,100>, a następnie wyświetli najpierw wszystkie wylosowane liczby parzyste, a potem liczby nieparzyste.


Odpowiedź :

Odpowiedź:

#include <iostream>

#include <ctime>

#include <cstdlib>

int main() {

   srand(time(nullptr));

   const int N = 20;

   int numbers[N];

   for(int i = 0; i < N; i++){

       numbers[i] = rand() % 100 + 1;

   }

   std::cout << "Liczby parzyste\n";

   for(const int &number : numbers){

       if(number % 2 == 0){

           std::cout << number << std::endl;

       }

   }

   std::cout << "Liczby nieparzyste\n";

   for(const int &number : numbers){

       if(number % 2 == 1){

           std::cout << number << std::endl;

       }

   }

   return 0;

}

Wyjaśnienie:

#include <iostream>

#include <cstdlib>

#include <time.h>

using namespace std;

int main()

{

   int pom, licznik = 0,s = 0;

   int tab[20];

   srand( time( NULL ) );

    cout << "Parzyste: ";

    for( int i = 0; i < 20; i++ ){

       pom = rand() % 100 + 1;

       if (pom % 2 == 0){

          cout << pom << ", ";

          licznik++;

       }

       else{

       tab[s] = pom;

       s++;

       }

    }

    cout << endl << "Nieparzyste: ";

    s = 0;

    while (20 - licznik  > s){

       cout << tab[s] << ", ";

       s++;

    }

   return 0;

}