webiz
asked on
Unable to understand while my while loop doesn't work (my first C exercise)
Hi.
Yes, a nb.
I have a problem with the following program I need to write as an essay.
assuming these are my variables:
startRange = 9
endRange = 20
I need to calculate which are the "powerfull" numbers, never mind what it means :)
the thing is that my while doesn't exit when it reaches the appropriate condition.
I am talking about the only while loop in this piece of code, so it won't be hard for you guys to find it :)
printf("Number : acceeding / powerfull\n");
for(currentNumber=startRan ge; currentNumber<=endRange; currentNumber++)
{
x=2;
while(currentNumber%x!=0 && x<currentNumber)
{
printf("%d",currentNumber) ;
printf("\n");
x++;
}
}
Yes, a nb.
I have a problem with the following program I need to write as an essay.
assuming these are my variables:
startRange = 9
endRange = 20
I need to calculate which are the "powerfull" numbers, never mind what it means :)
the thing is that my while doesn't exit when it reaches the appropriate condition.
I am talking about the only while loop in this piece of code, so it won't be hard for you guys to find it :)
printf("Number : acceeding / powerfull\n");
for(currentNumber=startRan
{
x=2;
while(currentNumber%x!=0 && x<currentNumber)
{
printf("%d",currentNumber)
printf("\n");
x++;
}
}
ASKER
well, it might look like it but it doesn't ;)
what I am getting is the following result:
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
15
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
and so on...
while I am supposed to get only
9
11
13
17
.....
what I am getting is the following result:
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
15
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
and so on...
while I am supposed to get only
9
11
13
17
.....
well when currentNumber is 11, x will loop from 2 to 10, and then exit the loop when x=11 and && x<currentNumber fails
each time through the loop, you print currentNumber, so you print it 11-2 = 9 times
Why are you supposed to get 9 and not 15?
each time through the loop, you print currentNumber, so you print it 11-2 = 9 times
Why are you supposed to get 9 and not 15?
what do u mean by powerfull numbers. if we know what it means it will be better to understand the problem
if you want powers of primes, then you should have included 16
you're printing "currentnumber" in the loop, so you'll see that value repeated as many times as the loop goes around.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if that is not what you wanted, do you consider to be appropriate condition?
It may be hard for you to see what x is in the loop since you never print it.