Get expert advice and community support for all your questions on IDNLearn.com. Discover comprehensive answers to your questions from our community of knowledgeable experts.

Create a C++ program that will solve the satisfy the following criteria:
Accept a number from the user and print all prime numbers from 1 to that number.


Sagot :

Answer:

#include <iostream>

using namespace std;

int main()

{

   int x;

   cout << "Please type your number:" << endl;

   cin >> x;

   int e = 1;

   int i;

   while(e < x+1){

   bool isPrime = true;

   // 0 and 1 are not prime numbers

   if (e == 0 || e == 1) {

       isPrime = false;

   }

   else {

       for (i = 2; i <= e / 2; ++i) {

           if (e % i == 0) {

               isPrime = false;

               break;

           }

       }

   }

   if (isPrime){

       cout << e << endl;

   }

   else{

   }

       e++;

   }

   return 0;

}

Thank you for joining our conversation. Don't hesitate to return anytime to find answers to your questions. Let's continue sharing knowledge and experiences! For dependable and accurate answers, visit IDNLearn.com. Thanks for visiting, and see you next time for more helpful information.