CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2010
    Posts
    75

    Arrow While loops to print even numbers

    Hi
    Good Morning Follows
    _________________

    am trying to write a program to get a number from user and then print even numbers as well as 0,2,4 and so on

    what changes i should do ? any tips or hints will be useful


    PHP Code:

    # include <iostream>
    using namespace std;
    int main () { 

        
    int n,i;

        
    cout << " Enter number ";
        
    cin >>n;

        
    0;

        while ( 
    i%== n) {
            
    cout <<i<<" ";
            
    i+1;
        }
        

        
    system ("PAUSE");
        return 
    0;


  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: While loops to print even numbers

    If you use your debugger and single step every line you will quite easily find what's wrong.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jul 2010
    Posts
    75

    Re: While loops to print even numbers

    the funny thing that am not that smart in math
    i know that if the number i put &#37; 2 ==0 i will get even right ?

    i will try and find out what wrongs thanks =)

  4. #4
    Join Date
    Jul 2010
    Posts
    75

    Re: While loops to print even numbers

    is this correct little or i make it worse ?!

    PHP Code:

    # include <iostream>
    using namespace std;
    int main () { 

        
    int n,i;

        
    cout << " Enter number ";
        
    cin >>n;

        
    0;

        while ( 
    == ) { 
            
    cout <<i<<" ";
        
    i+2;
        }

        
    system ("pause");
        return 
    0;


  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: While loops to print even numbers

    As I posted before, use the debugger. You will never learn how to program if your method is like:
    1. Throw a number of statements in the compiler/linkers face.
    2. If the compiler/linker accepts the statements ask why it's not working in a forum.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Jul 2010
    Posts
    75

    Re: While loops to print even numbers

    i use debugger there was no error ,, i know the mistake must be in the while loop
    am trying to understand it but i cant figue it out ,, fix my while loop statement !

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: While loops to print even numbers

    You use the debugger to check that every single line of code do what you expect it to do. If it doesn't you have an error.

    Check your while condition again. What results will you get from n % 2? How do those values compare to i? For what values of i will the while loop be executed? How does that conform with your intent?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: While loops to print even numbers

    [ Moved thread ]

  9. #9
    Join Date
    Jul 2010
    Posts
    75

    Re: While loops to print even numbers

    is that correct ?

    PHP Code:
    #include <iostream>
    using namespace std;

    int main()
    {
    int x 0;

    while (
    10)
    {
    cout << << endl;
    2;
    }
    system("pause");

    return 
    0;


  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: While loops to print even numbers

    Does it seem to work for you?

    (By the way, please indent your code properly. It will help you in the future.)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: While loops to print even numbers

    That depends on what you seek.
    That code will print out
    0
    2
    4
    6
    8
    so if that is what you want it is correct.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  12. #12
    Join Date
    Jan 2010
    Posts
    1,133

    Re: While loops to print even numbers

    As S_M_A said - if that's what you need, than it's correct.
    But, you know, if you want to write code that works, it's a good idea to know what the code you're writing actually does. Maybe I can help you a bit with that...
    Code:
        int n, i;                   // declares n and i as integer variables
    
        cout << " Enter number ";      // guess you know what this does
        cin >>n;                         // puts user input in n
    
        i = 0;                           // initializes i to 0
    
        while (condition) {             // repeats thing_that_needs_be_done as long as condition is TRUE
            thing_that_needs_be_done;    
        }
    Additional tips:
    • x % y - returns x mod y
    • x == y - returns TRUE or FALSE, indicating if x equals y or not, respectively
    • x = y - assigns the value of y to x. As you may have noticed, '=' is not the same as '=='...
    • x % y == z - returns returns TRUE or FALSE, indicating if x mod y equals z, or not.

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: While loops to print even numbers

    Quote Originally Posted by jacksparrow View Post
    Good Morning Follows
    I'm still waiting for the good morning.

    Do it in two steps. First write a while loop that loops all integers between 0 and the value the user entered. Then within this loop apply the even-test on every integer and print if applicable, like,

    First,

    Code:
        i = 0; 
        while (i <= n) { // i will assume all values between 0 and n.
            // print i for testing purposes
            i = i+1; 
        }
    Then,

    Code:
        i = 0; 
        while ( i <= n) {
            if (i&#37;2 == 0) { // check whether i is even (evenly divisible by 2)
               // print i
            }
            i = i+1; 
        }

  14. #14
    Join Date
    Jan 2010
    Posts
    1,133

    Re: While loops to print even numbers

    Quote Originally Posted by nuzzle View Post
    I'm still waiting for the good morning.
    It appears this makes you unusually pedagogical.

    Nice approach.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured