+ 3

How can you use the do/while loop to cout all the prime numbers until 100 in c++?

13th Jan 2017, 11:22 PM
Asim Maredia
3 Réponses
+ 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; }
13th Jan 2017, 11:28 PM
Filip
Filip - avatar
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
12th Apr 2017, 5:54 PM
Arun Kumar
Arun Kumar - avatar
0
Hi Asim! I would like to talk you ... would you like to add me on Facebook etc?
17th Jan 2018, 5:40 PM
Muhammad Shahbaz
Muhammad Shahbaz - avatar