How to stop the loop after printing one?

Viewed 514

So here is the problem: Write a program that accept an integer n, print out the largest number but smaller or equal n that is the product of two consecutive even number. Example: Input: 12, Output: 8 ( 2x4 ) Here is my code :

#include <stdio.h>

int main()
{
  int n;
  scanf("%d", &n);
  for (int i = n; i >= 0; i--)
      {
        for (int j = 0; j <= n; j = j + 2)
            {
              if ( i == j * (j+2) )
                  {
                    printf("%d ", i);
                    break;
                  }
            }
      }
return 0;
}

So if i input 20, it will print out 8 and 0 instead of 8, if i input 30, it will print out 24,8 and 0 instead of just 24. How do i make it stop after printing out the first number that appropriate ?

3 Answers

You need to stop an outer loop from processing, for example by using a boolean flag (meaning "solution found, we finish work") or a goto statement.

#include <stdio.h>

int main() {
  int n;
  scanf("%d", &n);
  int solutionFound = 0;
  for (int i = n; i >= 0; i--) {
      // this could also be put into for's condition i.e. "i >= 0 && !solutionFound"
      if (solutionFound) {
          break;
      }
      for (int j = 0; j <= n; j = j + 2) {
          if ( i == j * (j+2) ) {
              printf("%d ", i);
              solutionFound = 1;
              break;
          }
      }
  }
  return 0;
}

EDIT: immediate return as noted in the comments is also a nice idea, if you don't need to do anything later.

Break only breaks you out of immediate loop, so either use flags or just use return to terminate the execution. Or you can even use following code:

#include <stdio.h>

int main()
{
  int n;
  scanf("%d", &n);

        for (int j = 0; j <= n; j = j + 2)
            { 
              if ( n < j * (j+2) )
                  {
                    printf("%d ", j*(j-2));
                    break;
                  }
            }
return 0;
}

Your problem is that you are nested - in a for loop which is inside another for loop - when you want to stop processing.

Some languages would let you code break 2; to indicate that you want to break out of 2 loops. Alas, C i snot such a language.

I would recommend that you code a function. That would serve a few porpoises: 1) your main should be "lean & mean" 2) as your programs get larger, you will learn the benefits of putting individual coding tasks into functions 3) you can use return; instead of break; and it will exit the function immediately.

Something like this:

#include <stdio.h>

void FindNeighbouringDivisors(int n)
{
    for (int i = n; i >= 0; i--)
    {
        for (int j = 0; j <= n; j = j + 2)
        {
        if ( i == j * (j+2) )
          {
            printf("%d times %d = %d", j, j + 2, i);
            return;
          }
        }
    }
    
    printf("There are no two adjacent even numbers which can be multiplied to give %d", n);
}

int main()
{
  int n;
  scanf("%d", &n);      /* could get from comamnd line */
  
  FindNeighbouringDivisors(n);
  
  return 0;    /* should be EXIT_SUCCESS */
}

Btw, when you have a problem with your code, ask a question here. When you have it working, consider posting it at our code review site where more experienced programmers can give you advice on how to improve it. It's a great way to learn

Related