Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Unable to understand while my while loop doesn't work (my first C exercise)

Posted on 2006-11-15
7
216 Views
Last Modified: 2010-08-05
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=startRange; currentNumber<=endRange; currentNumber++)
                  {
                        x=2;
                        while(currentNumber%x!=0 && x<currentNumber)
                        {
                              printf("%d",currentNumber);
                              printf("\n");
                              x++;
                        }
                  }
0
Comment
Question by:webiz
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17951658
It looks like the while loop does exit when currentNumber%x ==0 or x>= currentNumber
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.
0
 
LVL 1

Author Comment

by:webiz
ID: 17951746
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
.....
0
 
LVL 84

Expert Comment

by:ozo
ID: 17951787
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?
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 2

Expert Comment

by:numansiddique
ID: 17953776
what do u mean by powerfull numbers. if we know what it means it will be better to understand the problem
0
 
LVL 84

Expert Comment

by:ozo
ID: 17953793
if you want powers of primes, then you should have included 16
0
 
LVL 22

Expert Comment

by:grg99
ID: 17954953
you're printing "currentnumber" in the loop, so you'll see that value repeated as many times as the loop goes around.

0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 250 total points
ID: 17955360
Hi webiz,

This might be a little clearer if the inner loop is rewritten.

               printf("Number : acceeding / powerfull\n");
               for(currentNumber=startRange; currentNumber<=endRange; currentNumber++)
               {
                    for (x=2; currentNumber%x!=0 && x<currentNumber; x++)
                    {
                         printf("%d\n",currentNumber);
                    }
               }

This is the exact loop that you execute in your example.  Perhaps you really mean to do something like this:

               printf("Number : acceeding / powerfull\n");
               for(currentNumber=startRange; currentNumber<=endRange; currentNumber++)
               {
                    for (x=2; x<currentNumber; x++)
                    {
                         if (currentNumber % x != 0)
                         {
                           printf("%d\n",currentNumber);
                           break;
                         }
                    }
               }


Good Luck,
Kent
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question