How to display only even numbered days?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How to display only even numbered days?

  1. #1
    Join Date
    Sep 2010
    Posts
    10

    How to display only even numbered days?

    I created the program below, but can't figure out how to get the test data to show 40 days worth, but only the EVEN days from 2 to 40.

    // Program created by MY NAME to determine a person's earnings in
    // pennies, by doubling each day, starting at 1 penny.
    #include <iostream>
    using namespace std;
    int main()
    {
    // Variables
    int days; // Each day worked.
    double final = 1,
    total = 0.00;

    // Asks for number of days worked to determine salary.
    cout << "Enter the number of days: ";
    cin >> days;

    for( int i = 1; i < ( days + 1 ); i++ )
    {

    cout << "Day " << i << ": " << "$" << final / 100 << endl;

    total += final;
    final += final;

    }

    cout << endl;
    cout << "Total: " << "$" << total / 100 << endl;

    return 0;

    }

  2. #2
    Join Date
    Sep 2010
    Posts
    10

    Re: How to display only even numbered days?

    Nevermind I figured it out. Just added the following line under my for statement.

    if(i % 2 == 0)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: How to display only even numbered days?

    Quote Originally Posted by JerBear24 View Post
    Nevermind I figured it out. Just added the following line under my for statement.

    if(i % 2 == 0)
    Why not just increment i by 2 every time?

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center