nested for loop problem

N

notahipee

Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.

#include <iostream.h>
#include <math.h>

int main ()
{
int a, b, c, d;

cout << "Enter two integers ";
cin >> a >> b;

for (c=a; c==b; c++)
{
for(d=2; d<=sqrt(c); d++)
{
if(c%d==0)
continue;

else cout << c;
}
}
cout << " are the prime numbers in the range " << a << " to " << b <<
endl;

return 0;
}
 
R

red floyd

notahipee said:
Would someone be able to tell me why this isn't working. The nested
Please read the FAQ 5.8
(http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8). Be a
bit more specific as to "this isn't working". What are your inputs?
What is your output? What is your *expected* output?

Note that from a cursory examination of the code, I could figure out
what "isn't working" means, but still...
for loops seem correctly coded to me. I would appreciate any input.

#include <iostream.h>
#include <math.h>

int main ()
{
int a, b, c, d;

cout << "Enter two integers ";
cin >> a >> b;

for (c=a; c==b; c++)
are you sure this is what you want? The loop will only execute as long
as the second statement is true. So if a and b are different, well...
nothing happens (which I assume is your complaint).
 
C

Christopher

I believe you wanted 'c<b' or 'c!=b'.

Additionally, you should be using the standard C++ headers:

#include <iostream>
#include <cmath>

using namespace std;

or qualify:
std::cin
std::cout
etc.
 
N

notahipee

Please read the FAQ 5.8
(http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8).  Be a
bit more specific as to "this isn't working".  What are your inputs?
What is your output?  What is your *expected* output?

Note that from a cursory examination of the code, I could figure out
what "isn't working" means, but still...






are you sure this is what you want?  The loop will only execute as long
as the second statement is true.  So if a and b are different, well...
nothing happens (which I assume is your complaint).






- Show quoted text -

I am trying to get the first loop to cycle from integer a to integer
b.
The second loop is supposed to test each integer for prime. The output
I intend is to print out only the primes. My opening cout and closing
cout work but the loops produced nothing.
 
N

notahipee

Additionally, you should be using the standard C++ headers:

#include <iostream>
#include <cmath>

using namespace std;

or qualify:
std::cin
std::cout
etc.

I tried c<=b and it works. However, it is couting duplicate,
triplicate, etc. in stead of just once each.
 
N

notahipee

I tried c<=b and it works. However, it is couting duplicate,
triplicate, etc. in stead of just once each.

For example if I enter 12 and 79 for a and b.
output reads 131314151617171718191919202121.....
 
N

notahipee

For example if I enter 12 and 79 for a and b.
output reads 131314151617171718191919202121.....

Oh never mind I just read the FAQ. Thank you for your help.
 
M

MiB

Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.

The algorithm you use for generating the primes is very inefficient.
However, bug fixing your code with minimal changes only, try this one.
It still misidentifies 0 and 1 as prime numbers (enter 0 as start
value...).

#include <iostream>

int main ()
{
unsigned int a, b;

std::cout << "Enter two integers ";
std::cin >> a >> b;

for ( unsigned int c = a; c <= b; c++ ) {
bool isprime = true;
for( unsigned int d = 2; d * d <= c; d++ ) {
if ( c % d == 0 ) {
isprime = false;
break;
}
}
if ( isprime ) std::cout << c << ' ';
}
std::cout << "are the prime numbers in the range " << a << " to " <<
b << std::endl;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top