hey there, i need help solvings my lab assignment in chapter 5: control structure II (repetition)

1. write a program that:

a. prompt the user to enter an integer number x greater than 1.
b. use a flag-controlled while loop to check whether the entered number x is prime or not. (a number is said to be prime if it is divisible only by 1 and by itself).

HINT: continue divising x until either the reminder is zero or you exceed x/2.

1. use a nested loop (for and while) to display all prime numbers less than 100.

Show what you have done so far?

I hate to do this, and I usually don't. But, the example you gave is off from what you gave in the example you gave. Please refer to this line: Here and this should answer your question.

Hope this helps

## All 6 Replies

Show what you have done so far?

``````#include <iostream>
using namespace std;
int main ()
{
int x;
cin>>x;
bool found=false;
while(!found)
{
if(x/1==x && x/x==x)
{
cout<<"the entered number is prime"<<endl;
continue;
}
else
}
cout<<"the entered number is not prime"<<endl;
continue;
}
}
while(x=found)
{
for (x=2; x<1; x++)
{
cout<<x<<" ";
cout<<endl;
}
}
return 0;
}{
``````

I hate to do this, and I usually don't. But, the example you gave is off from what you gave in the example you gave. Please refer to this line: Here and this should answer your question.

Hope this helps

Personally, I like the sieve of eratosthenes for most problems requiring evaluation of primes under 100,000,000,000 or so.

It's especially beautiful in its ease of implementation in any language.

``````#include <iostream>
using namespace std;
int main ()
{
for (int num=2; num<=100; num++)
{
int x=2;
bool prime= true;
cout<<"please enter a postive integer number: ";
cin>>x;
while (prime && x<=(num/2))
{
if (num %x==0)
prime= false;
else
x++;
}
if (prime)
cout<<"it's prime number."<<endl;
else
cout<<"it's not a prime number."<<endl;
}
return 0;
}
``````

is it correct?

or this?

``````#include <iostream>
using namespace std;
int main ()
{
int x, counter=2;
bool prime= true;
cout<<"please enter an integer number greater than 1: ";
cin>>x;
while (x<1)
{
cout<<"wrong number! x > 1, enter another integer again: ";
cin>>x;
}
while (prime && counter <x/2)
{
if(x%counter==0)
prime= false;
else
counter++;
}
if(prime)
{
cout<<x<<" is a prime number"<<endl;
}
else
{
cout<<x<<" not a prime number"<<endl;
}

for (int i=2; i<=100; i++)
{
while ( counter <= i/2)
{
if (i% counter ==0)
break;
counter++;
}
if (counter > i/2)
cout<<i<<" "<<endl;
}
cout<<endl;
return 0;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.