0

I just want to ask your help here. I am new beginner in c++ programming. How to print out the even number in range 100 - 200. I tried write some code and it didn't work out. Here is my code. I hope, someone here can help me. Will appreciate that so much. Thanks.

include <stdio.h>

void main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        print i;
    }
}
jfly
  • 7,715
  • 3
  • 35
  • 65
Patrick Wills
  • 43
  • 2
  • 2
  • 4

4 Answers4

4

Well, pretty simple:

#include <iostream> // This is the C++ I/O header, has basic functions like output an input.

int main(){ // the main function is generally an int, not a void.
   for(int i = 100; i <= 200; i+=2){ // for loop to advance by 2.
       std::cout << i << std::endl; // print out the number and go to next line, std:: is a prefix used for functions in the std namespace.
   } // End for loop
   return 0; // Return int function
} // Close the int function, end of program

you were using C libraries, not C++ ones, as well as no function that is called print in C++, nor C. Also there is no void main function, use int main() instead. finally, you need to have std:: in front of cout and endl as these lie in the std namespace.

Rivasa
  • 6,510
  • 3
  • 35
  • 64
  • You should use `std::cout` and `std::endl`. – scai Aug 09 '12 at 08:07
  • 1
    In fact, you *must* qualify the references with the `std` namespace in order for the code to compile. – Cody Gray - on strike Aug 09 '12 at 08:09
  • Most likely you don't need `std::endl` at each iteration either. – juanchopanza Aug 09 '12 at 08:12
  • @scai Why? I'm interested too as I sometimes see examples with `std::cout` but don't understand why - I've never come across problems just using `cout` –  Aug 09 '12 at 08:21
  • 1
    @duncan12 There is no "just `cout`". `cout` is defined in the `std` namespace. The only way it will work without fully qualifying the namespace reference is if you've written `using namespace std;` somewhere in your code, which is [generally considered bad form](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c), especially in headers. – Cody Gray - on strike Aug 09 '12 at 08:22
  • Hmm interesting. I've always just used `using namespace std;` and `cout`, `cin` etc. Not 100% convinced though as I'm not using two namespaces or anything.. –  Aug 09 '12 at 08:30
  • 1
    I'd suggest to declare `i` in the `for` loop. Having local iterators is one of the most convenient features of C++ (and C99). – Mehrwolf Aug 09 '12 at 10:44
  • @Mehrwolf oh, okay, this is what I do, since i first learned C89. – Rivasa Aug 09 '12 at 16:15
  • I did, I just kept that convention. There is nothing wrong with it, and it does the same thing. – Rivasa Aug 10 '12 at 00:02
0

Your code looks good....Only the printing part needs to be changed

  #include <stdio.h>
  int main()
  {
    for (int i= 100; i<= 200; i += 2){
      printf("%d",i);
    }
    return 0;
  }
Pawel Zubrycki
  • 2,703
  • 17
  • 26
Dilletante
  • 1,801
  • 4
  • 19
  • 18
  • ...and the part where he uses `void main()`, which doesn't exist in either C or C++. The `main` method always returns an `int`. – Cody Gray - on strike Aug 09 '12 at 08:09
  • @CodyGray True.... But even with a void main....You would get a warning.... The code would work fine – Dilletante Aug 09 '12 at 08:11
  • so is there any difference when use void main? – Patrick Wills Aug 09 '12 at 08:12
  • The code would not work fine, unless your compiler supported a void return type on main as an extension. GCC does not. It doesn't just give a warning, it gives an error. – Benjamin Lindley Aug 09 '12 at 08:15
  • 2
    `return 0` is in bad scope. It will just print 100 and exit. – Pawel Zubrycki Aug 09 '12 at 08:16
  • @MayankKhandelwal, the output is return 100. – Patrick Wills Aug 09 '12 at 08:16
  • @PatrickWills.... I guess pawel is right... it would depend on the compiler.... My compiler was passing it..... But i guess int main is a must then – Dilletante Aug 09 '12 at 08:18
  • Yes, getting the return type of the `main` method wrong invokes undefined behavior. That means what actually happens depends on your implementation. It could do anything it wanted. GCC just shakes its head at you, some older compilers provided an extension that would always return 0 to the OS if `main` returned `void`. That doesn't mean it wasn't wrong all along. As usual with undefined behavior, you shouldn't rely on it! – Cody Gray - on strike Aug 09 '12 at 08:24
0

Use the following code:

#include <iostream>

int main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        std::cout << i << std::endl;
    }
    return 0;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • No need to flush the output stream at each line. – juanchopanza Aug 09 '12 at 08:20
  • Oh oops deleted my prev. comment because it was answered elsewhere then you replied. But I've always done `using namespace std;` at the top then just `cout`. –  Aug 09 '12 at 08:31
0

This might help!

 #include <iostream>
using namespace std;
int main()
{
    for (int count = 100; count <= 200; count += 2)
    {
        cout << count << ", ";
    }
    cout << endl;
    return 0;

}