add by 4

Write your question here.

1
2
3
4
5
6
7
8
  int incrementer = 1;
for ( int i = 1; i < someLength; i += incrementer )
{
    cout << i << endl;
    ++incrementer;
}
//////_///_///////_/




looking for a way to increase i in a for loop by 4 up to 3000
Last edited on
I've tried swapping out the 1s for 4s...doesn't work.
You're close, your mistake was that you incremented the incrementer in the for loop

1
2
3
4
5
6
7
8
9
10
constexpr int incrementer = 4;
constexpr int someLength = 3000;

for (int i = 1; i < someLength; i += incrementer )
{
    cout << i << endl;

    // Don't do this!
    // ++incrementer;
}
Last edited on
if you want 4, 8, 12 ... then start at 4 (or maybe 0?) on line 4: for (int i = 4... )
you are gonna get 5, 9, ... as-is.
Okay, thank you. It works, Malibor...now I need to use i to recognize when a year(starting with 2020) is a multiple of 4. This is what I have...I'm definitely missing something.

if (year == 2020 || year == 2020 + i){
days_per_year = 366;
}
else {
days_per_year = 365;
}
cout << days_per_year;


as you can see, I'm hunting leap years
1
2
3
4
5
6
7
8
9
// Gregorian calender; extra leap day occurs in each year that is 
// an integer multiple of 4 (except for years evenly divisible by 100, but not by 400)
// https://en.wikipedia.org/wiki/Leap_year
bool is_leap( unsigned int year )
{
    if( year%400 == 0 ) return true ; // leap year if multiple of 400
    else if( year%100 == 0 ) return false ; // not leap year if multiple of 100, but not multiple of 400
    else return ( year%4 == 0 ) ; // else leap year if multiple of 4
}
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
	for (unsigned y { 2020 }; y <= 3000; y += 4)
		if ((y % 100) || (y % 400 == 0))
			std::cout << y << ' ';

	std::cout << '\n';
}

Thanks, seeplus...this works, but it only seems to spit out leap years. I also need it to print whether there are 365 or 366 days in a user inputted year. My code above accomplished that, it just can't find the leap years like yours can. Is there a way to marry the two?

Nevermind...I figured it out. Thanks for your help!
Last edited on
You didn't say about user inputted year. You only mentioned years 2020 to 3000.

If you want to use it with a given year, then use JLBorges function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

bool is_leap(unsigned year) {
	if (year % 400 == 0) return true; // leap year if multiple of 400
	else if (year % 100 == 0) return false; // not leap year if multiple of 100, but not multiple of 400
	else return (year % 4 == 0); // else leap year if multiple of 4
}

int main() {
	unsigned year {};

	std::cout << "Enter a year: ";
	std::cin >> year;

	std::cout << year << " has " << 365 + is_leap(year) << " days\n";
}


This std::chrono thingy is a bit weird.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

bool is_leap( unsigned y )
{
   return chrono::year(y).is_leap();
}

int main()
{
   for (int y = 1900; y <= 2100; y++ ) cout << y << '\t' << boolalpha << is_leap( y ) << '\n';
}


or, the hard way,

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

bool is_leap( unsigned y )
{
   using namespace std::chrono;
   return year_month_day_last(year(y),month_day_last(month(February))).day() == day(29);
}

int main()
{
   for (int y = 1900; y <= 2100; y++ ) cout << y << '\t' << boolalpha << is_leap( y ) << '\n';
}
Last edited on
Topic archived. No new replies allowed.