-
October 10th, 2010, 08:05 PM
#1
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;
}
-
October 10th, 2010, 08:47 PM
#2
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)
-
October 10th, 2010, 08:56 PM
#3
Re: How to display only even numbered days?
Originally Posted by JerBear24
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|