Va rog sa ma ajutati daca stiti.
Răspunsuri la întrebare
#include <iostream>
using namespace std;
//Functie numar prim
int nr_prim(int x)
{
if(x<2)
return 0;
if(x%2 == 0)
return 0;
for(int i=3;i*i<=x;i+=2)
if(x%i == 0)
return 0;
return 1;
}
//functie numere prime pana la n
void nr_prime()
{
int n;
cout<<"n= ";
cin>>n;
for(int i=1;i<=n;i++)
if(nr_prim(i))
cout<<i<<' ';
}
//int principal
int main()
{
nr_prime();
return 0;
}
Daca ai nevoie de o varianta mai de gimnaziu, scrie-mi in comentariu
Varianta a II-a:
#include <iostream>
using namespace std;
int main()
{
int n;
int x = 2;
int ok;
cout<<"n= ";
cin>>n;
cout<<x<<' ';
while(x<=n)
{
ok = 1;
if(x<2)
ok = 0;
if(x%2 == 0)
ok = 0;
for(int i=3;i*i<=x;i+=2)
if(x%i == 0)
{
ok = 0;
i = x;
}
if(ok == 1)
cout<<x<<' ';
x++;
}
}
using namespace std;
int main()
{
int n;
int x = 2;
int ok;
cout<<"n= ";
cin>>n;
while(x<=n)
{
ok = 1;
if(x<2)
ok = 0;
if(x%2 == 0)
ok = 0;
for(int i=3;i*i<=x;i+=2)
if(x%i == 0)
{
ok = 0;
i = x;
}
if(ok == 1)
cout< x++;
}
}
using namespace std;
int main()
{
int n;
int x = 2;
int ok;
cout<<"n= ";
cin>>n;
cout<
while(x<=n)
{
ok = 1;
if(x<2)
ok = 0;
if(x%2 == 0)
ok = 0;
for(int i=3;i*i<=x;i+=2)
if(x%i == 0)
{
ok = 0;
i = x;
}
if(ok == 1)
cout< x++;
}
}