how to write a code that prints the next 20 leap years using a for loop

can someone help me Write a program that prints the next 20 leap years. ( You must use a for loop in your code )
Last edited on
Start at the year 2016 and keep incrementing by one.

What is a leap year?
Years divisible by 4 are leap years, except for those that are also divisible by 100. Years divisible by 400 are leap years regardless.

So you will need a series of if-statements in your loop to decide whether you will print the current year or not. Have a counter variable that you increment every time you print a leap year. Once that variable reaches 20, you can end the loop.

Write the pseudo code first based on the rules above, figure out how you can do this using a for-loop, and then translate to C++ code. Post here if you get stuck.
Last edited on
@Arslan

great response!
#include <iostream>

using namespace std;

int main()
{
int yr=2016;
for(int i=0;i<20;i++)
{

cout<<yr<<endl;
yr=yr+4;
}
return 0;
}
This is the sample code you can use, may modify as you want
thank you
Topic archived. No new replies allowed.