English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include

int main() {
int num, num2,a,b;
do{
cout << "Enter a value in the range of 1 to 100: ";
cin >> num;
if(num < 1 || num > 100)
cout << "Out of range; Please try again... " << endl
<< endl;
}while (num < 1 || num > 100);

int counter = 0;
for (a = 0; a <= num; a++){
}
for (b = 1; b < a; b++){
if (a % b == 0)

counter++;

if (counter == 1)

cout<< "The prime numbers are " << a << " ";
}
return 0;
}

Enter a value from 1 to 100: -5
Out of range; Please try again...

Enter a value from 1 to 100: 200
Out of range; Please try again...

Enter a value from 1 to 100: 101
Out of range; Please try again...

Enter a value from 1 to 100: 45

The prime numbers are:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43

This is what the output supposed to look like, but when I ran my code, the prime number output does not come out right. can anyone help me fix the problem?

2007-03-29 19:11:33 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

I am supposed to ask a user to enter a number from 1 to 100, and loop it until the user enters the value in the right range. Then, once the user enters the right number, I am supposed to find every prime number upto that number the user entered.

2007-03-29 19:13:06 · update #1

4 answers

Here is a cleaner version of your code:

#include

// The following functions works on positive integers greater
// than zero only

bool isPrime(int number);

int main()
{
int num= 0;

do
{
cout << "Enter a value in the range of 1 to 100: ";
cin >> num;

if(num < 1 || num > 100)
cout << "Out of range; Please try again... " << endl
<< endl;

}while(num< 1 || num > 100);

cout<< "The prime numbers are: " << endl;
for(int i = 1; i < num; i++)
{
if(isPrime(i))
cout << i << " ";
}

cout << endl;

return 0;
}

bool isPrime(int number)
{
for(int i = 2; i < number - 1; i++)
{
if((number % i) == 0)
return false;
}
return true;
}

2007-03-29 19:50:07 · answer #1 · answered by Silver_Sword 3 · 0 1

First you haven't initialised your variable so it might already have a number value within the range. Set it to zero before you start. num=0;

It also looks like your program will only run through the counter and then output prime numbers if the counter = 1. You need to output your numbers from within a loop so that they are output each time you reach one.

2007-03-30 02:18:47 · answer #2 · answered by chekeir 6 · 0 1

heres the problem with your code. you wrote:

int counter = 0;
for (a = 0; a <= num; a++){
}
for (b = 1; b < a; b++){
if (a % b == 0)

counter++;

if (counter == 1)

cout<< "The prime numbers are " << a << " ";
}


your first "for" loop does nothing because there is nothing between the brackets.
the loop is:
for (a = 0; a <= num; a++){
}


i think what you wanted is this:
for (a = 0; a <= num; a++)
__{
__for (b = 1; b < a; b++)
____{
____if (a % b == 0)
______counter++;
____if (counter == 1)
______cout<< "The prime numbers are " << a << " ";
____}
__}

2007-03-30 09:12:46 · answer #3 · answered by justme 7 · 0 1

oh

big mistake


A & B is missing before C


bye

good night

2007-03-30 02:14:13 · answer #4 · answered by jay Z 4 · 0 1

fedest.com, questions and answers