1//sieve of eratosthenes or prime of sieve
2#include<iostream>
3#include<math.h>
4using namespace std;
5void primeofsieve(long long int n)
6{
7 long long int arr[n]={};
8 for(int i=2;i<=sqrt(n);i++)
9 {
10 for(long long int j=i*i;j<=n;j+=i)
11 arr[j]=1;
12 }
13 for(long long int i=2;i<=n;i++)
14 {
15 if(arr[i]==0)
16 cout<<i<<" ";
17 }
18
19
20}
21int main()
22{
23
24 #ifdef _DEBUG
25 freopen("input.txt", "r", stdin);
26 freopen("output.txt", "w", stdout);
27 #endif
28 long long int n;
29 cin>>n;
30 cout<<"PRIME NUMBERs ARE : ";
31 primeofsieve(n);
32 return 0;
33}
1/******************************************************************************
2
3 Online C Compiler.
4 Code, Compile, Run and Debug C program online.
5Write your code in this editor and press "Run" button to compile and execute it.
6
7*******************************************************************************/
8
9#include <stdio.h>
10#include <math.h>
11#include <stdbool.h>
12
13int main()
14{
15 int n,i,j; //declaring required variables
16 scanf("%d",&n); //scanning n(limit numbers)
17 bool arr[n+1]; //declaring an array which contains true and false values
18 arr[1]=false; //setting the first index to false (1 is not a prime number)
19
20 for(i=2;i<=n;i++){
21 arr[i]=true; //we are assuming that all the numbers are true which means prime
22 }
23
24 for(i=2;i<sqrt(n)+1;i++){ //running loop to the sqrt on n + 1
25 if(arr[i]==true){ //if the number is true of prime
26 for(j=i+i;j<=n;j+=i){ //run the loop to n
27 arr[j]=false; //assume that the divisble numbers are not prime
28 }
29 }
30 }
31
32 for(i=2;i<=n;i++){
33 if(arr[i]==true){
34 printf("%d ",i); //printing the numbers if that is true
35 }
36 }
37
38
39
40 return 0;
41}
42
43