At Westonci.ca, we connect you with experts who provide detailed answers to your most pressing questions. Start exploring now! Discover detailed answers to your questions from a wide network of experts on our comprehensive Q&A platform. Explore comprehensive solutions to your questions from a wide range of professionals on our user-friendly platform.

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;

}