-3

I just started to study C++, and I want to ask why my simple code's output is not right.

What I want:

user input N -> output = " N number that mod 2 =0 but not mod 3=0"

What I got:

user input N -> output = " number that mod 2 but not mod3=0 , with range until n "

Here is my code:

#include <iostream>
#include <conio.h>

int main()
{
    int i,n;

    std::cout << "input n" << std::endl;
    std::cin >> n;
    std::cout << "N Number that mod2=0 but mod3!=0" << std::endl;

    for ( i = 1; i <= n; ++i )
    {
        if ( i % 2 == 0 && i % 3 != 0 )
        {
            std::cout << i < "   ";
        }
    }

    getch ();
}
DevSolar
  • 67,862
  • 21
  • 134
  • 209
Atika
  • 13
  • 3
  • 1
    Do you want to just do the check only for the number n? I didn't understand your question correctly. – Vasanth Apr 06 '16 at 14:36
  • 3
    Your program cannot generate your desired output. It also cannot generate the observed output. Simply because the string in the source you posted is different to either of them. Please double-check that the program you are running is actually the one compiled from the source you posted. Then try to formulate your question in a better way; right now it is not clear what you want. – DevSolar Apr 06 '16 at 14:37
  • So you want to display all the numbers between 1->N that are i%2=0 but not i%3 = 0? – Khalil Khalaf Apr 06 '16 at 14:39
  • I edited your question. Take note of the changes I did to the code; there is a full handfull of good practices applied there. It also makes a certain code error more obvious, which I left in there. Can you spot it? It's in the `if()` body. I assume it's a copy & paste error, because your compiler should have thrown a fit otherwise. – DevSolar Apr 06 '16 at 14:45
  • @Vasanth no.. for example ,, input : 5 , Output : 2,4,8,10,14 not input:5 , output : 2,4. – Atika Apr 06 '16 at 14:46
  • This code doesn't even compile. Please do a clean copy and paste from your editor, otherwise we all just waste our time. For example, what about the "with range until n " message? Does it have to be printed? What you are asking isn't clear. – Fabio says Reinstate Monica Apr 06 '16 at 14:49

3 Answers3

1

If I understand you correctly you want the user to input the amount of numbers that fulfill your condition. For that you should have a counter:

#include <iostream>
#include <conio.h>
#include <cmath>

using namespace std;

int main()
{
    int n;

    cout << "input n" << endl;
    cin >> n;
    cout << n << " numbers for that holds that mod2 = 0 but mod3 != 0" << endl;

    int counter = 0; 
    for (int i = 1; counter < n; ++i)
    {
        if (i % 2 == 0 && i % 3 != 0)
        {
            cout << i << "   ";
            ++counter;
        }
    }
    getch ();
}

I also changed some other details.

IceFire
  • 4,016
  • 2
  • 31
  • 51
1

Different things to take into mind:

  • It is better to include from <iostream> iso <iostream.h> (will add link for why)
  • cout, cin and endl are in the std namespace, so either use the correct namespace or add std::
  • The return type of main() should be int. If there is no return statement it will be 0 implicitly.
    • There's a difference between operator< and operator<<

Code:

    #include <iostream>

    int main()
    {
        int i,n;

        std::cout<<"input n"<<std::endl;
        std::cin>>n;
        std::cout<<"N Number that mod2=0 but mod3!=0"<<std::endl;

        for (i=1;i<=n;i++)
            if (i%2==0 && i%3!=0)
                std::cout << i << std::endl;

        return 0;
    }
Zafi
  • 619
  • 6
  • 16
  • `main`does not need to return an `int`, only the return type has to be `int` – IceFire Apr 06 '16 at 14:47
  • C++ Standard, chapter 3.6.1 paragraph 5: A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0; So it will return an int implicitly ;). – Zafi Apr 06 '16 at 14:50
  • Exactly. Your third bullet point together with `return 0;` implicated to me, though, that `return 0;` is a necessary statement, which it is not. I just wanted to clarify this with my comment – IceFire Apr 06 '16 at 14:52
  • 1
    Ok, I agree that it might be confusing. I'll edit the answer. – Zafi Apr 06 '16 at 14:55
0

Not sure, I got the question completely, but the most evident culprit is the typo in the line:

cout<<i<"   ";

Replace "<" with stream output operator, that is "<<".

Anatoly
  • 21
  • 3