answersLogoWhite

0

#include<iostream>

#include<cmath>

#include<utility>

bool is_prime (const unsigned n) {

if (n<2) return false;

if (!(n%2)) return n==2;

unsigned max_factor=(unsigned)sqrt((double)n)+1;

for (unsigned factor=3; factor<max_factor; ++factor) {

if (!(n%factor)) return false;

}

return true;

}

void print_primes(const unsigned low, const unsigned high) {

for (int i=low; i<=high; ++i) {

if (is_prime (i)) std::cout<<i<<std::endl;

}

}

int main () {

unsigned n, x;

std::cout<<"Print prime numbers\nFrom: ";

std::cin>>n;

std::cout<<"To: ";

std::cin>>x;

if (x<n) std::swap(x,n);

print_primes (n, x);

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

How do you print non-prime numbers in java?

Loop through some numbers - for example, 2 through 100 - and check each one whether it is a prime number (write a second loop to test whether it is divisible by any number between 2 and the number minus 1). If, in this second loop, you find a factor that is greater than 1 and less than the number, it is not a prime, and you can print it out.


What BASIC program can compute and display all prime numbers from 1 to 40?

PRINT 2,3,5,7,11,13,17,19,23,29,31,37


Python function that lists prime numbers?

If you just want a hint: One way to check whether a number is prime is by dividing it by any number between 2 and the square root of your number. If the number divides by any of these, it is not prime. If you want the code: import math for num in range(1,101): if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)): print num


Write a c program to find out the prime numbers between 1 to 500?

To write a C program to find prime numbers between 1 to 500, you can use a nested loop structure. In the outer loop, iterate from 2 to 500, and in the inner loop, check if the number is divisible by any number from 2 to the square root of the number. If it is not divisible by any number other than 1 and itself, then it is a prime number. Print out all prime numbers found within the specified range. Remember to include necessary header files, such as &lt;stdio.h&gt;, and use appropriate logic to implement the program efficiently.


Is 38 a composite number?

Yes. All non-prime numbers are composite numbers, which simply means a number has one or more prime factors other than 1 and the number itself.

Related Questions

How many prime numbers between 1 and 8888888888888888888888888888888888888888888888?

To determine the number of prime numbers between 1 and 8888888888888888888888888888888888888888888888, we can use the Prime Number Theorem. This theorem states that the density of prime numbers around a large number n is approximately 1/ln(n). Therefore, the number of prime numbers between 1 and 8888888888888888888888888888888888888888888888 can be estimated by dividing ln(8888888888888888888888888888888888888888888888) by ln(2), which gives approximately 1.33 x 10^27 prime numbers.


Who is the inventor of prime numbers?

Mathematics, including prime numbers, is discovered, not invented.Systems and methods we use are invented, but concepts of relationships between objects governed by logic, such as the prime numbers are discovered and named. As such, a more appropriate question might be "Who discovered prime numbers?"Many have discovered prime numbers; the first is unknown to mankind.


How can print prime numbers in between 1-100 but not adjust 40-50 in php language?

Use a counted loop in the closed range [1:100]. If the count is in the closed range [40:50], print the number. For all other numbers outwith this range, only print the number if it is prime.


What is the product of the prime numbers between 29 and 30?

29 is a prime number. There are no prime numbers between 29 and 30.


Are there less prime numbers between 101 and 200 than between 1 and 100?

NO. There are more prime numbers between 1 and 100 than the prime numbers between 101 and 200.number of prime numbers between 1 and 100 = 25number of prime numbers between 101 and 200 = 20


Are there prime numbers between 114 and126?

There are no prime numbers between 114 and 126.


What are the prime numbers between 33 and 36?

There are no prime numbers between 33 and 36.


What are the prime numbers between 41 and54?

The prime numbers between 41 and 54 are 43,47,53.


What are the prime numbers between 12 and 48?

The prime numbers between 12 and 48 are 13,17,19,23,29,31,37,41,43,47.


What are the prime numbers between 31 and 50?

The prime numbers between 31 and 50 are 37,41,43,47.


How many prime numbers are in between 71 to 80?

Prime numbers between 71 to 80 = 73 and 79 Therefore there are 2 prime numbers between 71 to 80.


How do you tell between prime and composite numbers?

Prime numbers have no divisibles.