Create a program that computes all prime numbers le ss than

Create a program that computes all prime numbers le

ss than a

max_number

(input by the user

but no more than 50,000) by storing them in an arra

y and then printing them 10 primes per row.

A prime is a positive integer greater than 1 whose

only factors are 1 and itself.

1.

Create a void function to get the user input.

2.

Code the loop to obtain the number of primes from 1

max_number

3.

Declare the array with the

exact number of cells

as primes from 1 –

max_number.

4.

Load the primes into the array, one by one.

5.

Create a void function to output the values to the

screen using this array.

6.

Test your program with the following inputs

and

another one of your choice

234 2008 8888 19893 29001

43789

Notes:

1.

You cannot declare a bigger array then the number o

f primes from 1 – max_number

2.

Use the isprime function below:

bool is_primen(long n)

{

int div = 1;

while (++div <= sqrt(n) )

if( n % div == 0) return false;

Solution

The below is the program for printing the prime numbers that we get less than of equal to the given user number .

We are implementing this program using C++ language

Here we are using STACK using array\'s to store the prime numbers one by one

we are taking the length of the array as the maximum length of the prime numbers that can be obtained from the maximum limit of 50,000 this can be done by calculating x/(logx-1)(x is 50,000 in our case ) from prime number theorem . here in our case the value is 5200

we are using \" add \" Function to add the prime numbers obtained into the stack array

we are using \" is_primen\" function to check for the prime factor logic

we are using \" display \" function to display the elements in the stack i.e prime numbers

//Program for determining prime number less then 50000
#include<iostream>
#include<cmath>

using namespace std;

int top=-1; /* Global variable decleration for the covience */
int stack[5200];

void add(long a)

{
if(stack[top]<5200)
top++;
stack[top]=a;
}

bool is_primen(long n)
{
int div = 1;
while (++div <= sqrt(n) )
if( n % div == 0)
return false;
add(n);
return true;
}

void display()

{
int a,i=0;
for(a=0;a<=top;a++){
cout<<\" \"<<stack[a]<<\",\";
i++; /*here i is used to count the number upto 10 so that we could print 10 number per row */
if(i%10==0)
cout<<endl;
}
}

int main()

{
long n=0,j,a;
cout<<\"\ Enter a number:\"<<endl;
cin>>n;
for(j=1;j<n;j++){
a=is_primen(j);
}
display();
return 0;
}

OUTPUT:

Enter a number :

234

1,2,3,5,7,11,13,17,19,23,

29,31,37,41,43,47,53,59,61,67,

71,73,79,83,89,97,101,103,107,109,

113,127,131,139,149,151,157,163,167,

173,179,181,191,193,197,199,211,223,227,229,233

This is the implementation of the given program using the language C++ .

Create a program that computes all prime numbers le ss than a max_number (input by the user but no more than 50,000) by storing them in an arra y and then print
Create a program that computes all prime numbers le ss than a max_number (input by the user but no more than 50,000) by storing them in an arra y and then print
Create a program that computes all prime numbers le ss than a max_number (input by the user but no more than 50,000) by storing them in an arra y and then print

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site