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

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
RossRoss
Every question is just a happy little opportunity.
Chat with Ross

Add your answer:

Earn +20 pts
Q: Print all prime numbers between two unknown numbers n and x?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.