5 to a number

Write your question here.
Def: A year is a century year if it is divisible by 100{ex: 100,200}
Def: A year is non- century year if it is not a century year.{ex: 1,2,3}
Def: A year is a leap year if it is a non -century year that is divisible by 4 or a century year divisible by 400 . Nothing else is a leap year.

Write a program that is going to ask the user for the range of years and print out
all leap years in that range 5 to a line ?
I am not able to find how to print 5 numbers to a line
here is my code

//leapyear.cpp
#include <iostream>
using namespace std;

int main ()
{
int n1,n2,counter;
cout << "Please give me a range of year:";
cin >> n1 >> n2;
if(n1<1 || n2<1)
{
cout << "Does not exist";
return 0;
}
for(int c = n1;c<=n2;c++)
{
for(int i = 0 ;i<5;i++)

if((c%100 !=0 && c%4 ==0) || (c%100 == 0 && c%400 == 0))
cout << c << " ";

cout << endl;


}
cout << "These all are leap years in the given range";
cout << endl;
return 0;
}
Last edited on
closed account (3qX21hU5)
Well lets look at how we print 1 number to a lone.

cout << numberOne;. That is simple enough. To print 5 numbers per line it is actually just as simple as that.

cout << numberOne << " " << numberTwo << " " << numberThree << endl;

Now that only prints 3 numbers then creates a new line but I believe you get the idea. We at in the blank " " to create spaces between each number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//leapyear.cpp
#include <iostream>
using namespace std;

int main ()
{
    int n1,n2,counter=0;
    cout << "Please give me a range of year:";
    cin >> n1 >> n2;
    if(n1<1 || n2<1)
    {
        cout << "Does not exist";
        return 0;
    }
    for(int c = n1;c<=n2;c++)
    {
        if((c%100 !=0 && c%4 ==0) || (c%100 == 0 && c%400 == 0))
        {
            if(counter<5)
            {
                cout << c << " ";
                counter++;
            }
            else
            {
                cout << endl;
                counter=0;
            }
        }
    }
    cout << "\nThese all are leap years in the given range";
    cout << endl;
    return 0;
}
@ahmarajeel

Here is your program, and it checks for valid responses by letting you know what's wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//leapyear.cpp
#include <iostream>

using namespace std;

int main ()
{
	int n1,n2,counter = 0;
	
	do
	{
		cout << "Please give me a range of years:" << endl;
		cout << "Starting year : ";
		cin >> n1;
		cout << "Ending year : ";
		cin >> n2;
		if(n1<0 || n2<0)
			cout << "Leap years did NOT even exist then. Please try again." << endl;
		if (n1>n2)
		{
			cout << "Ending year MUST be greater than the starting year!!" << endl;
			n1=0,n2=0;
		}
	} while (n1<1||n2<1||n1>n2);
	for(int year = n1; year<=n2; year++)
	{
		if((year%100 !=0 && year%4 ==0) || year%400 == 0)
		{
			counter++;
 			cout << year << " ";
			if (counter == 5)
			{
				counter = 0;
				cout << endl;
			}
			
		}
	}
	cout << endl << endl << "These all are leap years in the given range";
	cout << endl;
	return 0;
}
Thanks you very much Zereo Chriscpp Whitenite.

Topic archived. No new replies allowed.