Home > OS >  If statement inside of a for cycle
If statement inside of a for cycle

Time:04-10

first of all, sorry if my title isn't appropriate. English isn't my first language so I didn't really know how to phrase it correctly and understandably.

So, I'm learning some C and I got stuck on a problem. Right now my code looks like this:

#include <iostream>

using namespace std;

int main()
{
    int start, finish;

    cout << "Enter range start" << endl;
    cin >> start;
    cout << "Enter the range end" << endl;
    cin >> finish;

    if (start < finish)
    {
        for (int i = start; i <= finish; i  )
        {
            cout << "Number " << i << " is divisible by ";
            for (int j = 2; j <= 10; j  )
            {
                if (i % j == 0)
                {
                    cout << j << " ";
                }
            }
            cout << endl;
        }
    }
    return 0;
}

And the output I get is:

Number i is divisible by j1, j2 and etc.
Number i is divisible by
Number i is divisible by j1, j2 and etc.

You get the idea. What I'm trying to get is this

Number i is divisible by j1, j2 and etc.
Number i isn't divisible by any numbers
Number i is divisible by j1, j2 and etc.

I tried doing this:

#include <iostream>

using namespace std;

int main()
{
    int start, finish;

    cout << "Enter range start" << endl;
    cin >> start;
    cout << "Enter the range end" << endl;
    cin >> finish;

    if (start < finish)
    {
        for (int i = start; i <= finish; i  )
        {
            cout << "Number " << i;
            for (int j = 2; j <= 10; j  )
            {
                if (i % j == 0)
                {
                    cout << " is divisible by " << j << " ";
                }
                else
                {
                    cout << " isn't divisible by any numbers";
                }
            }
            cout << endl;
        }
    }
    return 0;
}

But the output is just a mess. Sorry if this is something straight forward and something I could've looked up on the net, but I just didn't know how to phrase my problem correctly and what to look for. I'll appreciate any help.

CodePudding user response:

You should

  1. create a flag that indicates if any j that divides i.
  2. after the loop, check the flag and print the message if applicable.
#include <iostream>

using namespace std;

int main()
{
    int start, finish;

    cout << "Enter range start" << endl;
    cin >> start;
    cout << "Enter the range end" << endl;
    cin >> finish;

    if (start < finish)
    {
        for (int i = start; i <= finish; i  )
        {
            cout << "Number " << i;
            bool divisorFound = false;
            for (int j = 2; j <= 10; j  )
            {
                if (i % j == 0)
                {
                    if (!divisorFound)
                    {
                        cout << " is divisible by ";
                    }
                    cout << j << " ";
                    divisorFound = true;
                }
            }
            if (!divisorFound)
            {
                cout << " isn't divisible by any numbers";
            }
            cout << endl;
        }
    }
    return 0;
}
  •  Tags:  
  • c
  • Related