+ 3
How can you use the do/while loop to cout all the prime numbers until 100 in c++?
3 odpowiedzi
+ 11
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(unsigned n) {
for(unsigned i=2; i<=sqrt(n); i++)
if(n%i==0)
return 0;
return 1;
}
int main() {
unsigned x=2;
do {
if(isPrime(x))
cout<<x<<endl;
x++;
} while(x<100);
return 0;
}
0
Here is a Algorithm to print all prime numbers between 1 to N
For every number i, between 2 to N/2(2<= i <= N/2) check whether i divides N completely(check If i is a factor of N).
If none of the number between 2 to N/2 divides N completely then N is a prime number.
Source : http://www.techcrashcourse.com/2016/04/cpp-program-print-all-prime-numbers-1-to-N.html
http://www.techcrashcourse.com/2015/11/c-program-to-print-all-prime-numbers-between-1-to-N.html
0
Hi Asim!
I would like to talk you ...
would you like to add me on Facebook etc?