Answer to Question #284713 in C++ for papi

Question #284713

To print the prime numbers between 1 and 1000.


1
Expert's answer
2022-01-11T02:16:08-0500
#include <iostream>
#include <math.h>
using namespace std;


bool isPrime(int num)
{
	if (num < 2)
	{
		return false;
	}
	if (num == 2 || num == 3)
	{
		return true;
	}
	for (int i = 2; i <= sqrt(num * 1.0); i++)
	{
		if (num % i == 0)
		{
			return false;
		}
	}
	return true;
}




int main() {
	int number=1;
	cout<<("The first 10 prime numbers are:");
	while (number <=1000) {
		if (isPrime(number)) {
			cout<<number<<"\n";
		}
		number++;
	}


	cin>>number;


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog