0
How can we print prime no. Between 1 to n in c++?
Hi sololearn community, plzz help
7 Answers
+ 4
One way:
1. Write a function that determines for a positive integer n whether or not it is prime.
2. Run a loop from 1 to n and cout n if n is prime using the function from step 1.
Another way:
1. Create an array of size n and run a prime sieve algorithm (cf. Eratosthenes prime sieve)
2. Loop over array and print if indicated to be prime.
+ 2
Thanks,
Ani Jona š for suggesting desired algorithm.
I have understood the Algo how it works
But somewhere struggling with the code
Could you please guide me further.
Big thanks to you.
+ 2
I may of you give me some code to look at š
+ 1
#include<iostream>
using namespace std;
int main() {
int i,n;
int arr[1100;
cin>>n;
for(i=0; i<n; i++) {
arr[i]=i+1;
}
for(i=0; i<n; i++) {
cout<<"the arr is: "<<arr[i]<<endl;
}
return 0;
}
I have written a code for taking n input from user and string them in array of size 100 which is working fine.
And i found a piece of code on internet
Sharing with you.
+ 1
References - geeksforgeeks
+ 1
It seems that the GfG code example solves your problem :)