0

How can we print prime no. Between 1 to n in c++?

Hi sololearn community, plzz help

16th Apr 2022, 4:21 PM
Bhupendra Dangwal
Bhupendra Dangwal - avatar
7 Réponses
+ 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.
16th Apr 2022, 5:16 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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.
16th Apr 2022, 7:46 PM
Bhupendra Dangwal
Bhupendra Dangwal - avatar
+ 2
I may of you give me some code to look at 🙂
16th Apr 2022, 7:47 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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.
16th Apr 2022, 7:54 PM
Bhupendra Dangwal
Bhupendra Dangwal - avatar
16th Apr 2022, 7:58 PM
Bhupendra Dangwal
Bhupendra Dangwal - avatar
+ 1
References - geeksforgeeks
16th Apr 2022, 7:59 PM
Bhupendra Dangwal
Bhupendra Dangwal - avatar
+ 1
It seems that the GfG code example solves your problem :)
17th Apr 2022, 4:05 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar