dimanche 10 janvier 2021

delete a prime number c++

Hei , guys! I have 1 problem in c++ who says if i have a vector with n elements , i need to delete prime numbers and display what's left.

    #include <iostream>
#include <cmath>
using namespace std;
    int main()
    {
        
        int v[1010]; // initialize the vector;
        int n, nr=0;
        cin >> n; // read the n elements
        for (int i = 0; i < n; i++)
        {
            cin >>  v[i] ; // read the vector
        }

        for (int i = 0; i < n; i++)
        {
            nr = 0;
            
            for (int j = 2; j < v[i] / 2; j++)
            {
                if (v[i] % j == 0 && v[i] >0 ) // if the rest of division of v[i] at j is 0 and v[i] >0
                {
                    nr++;
                }
                
            }
            if (nr != 0 ||v[i]==1) 
            {
                cout << v[i] << " "; // display the vector with what's left
            }
        }


        return 0;
    }
   

This code makes only 60/100 % and i don't understand why ?

Aucun commentaire:

Enregistrer un commentaire