idk what is it

1
2
3
4
5
6
7
8
9
10
11
12
// Accepts a year
// Returns 0 if it is NOT a leap year
// Returns 1 if it IS a leap year
// Returns 2 if it is out of bounds
int is_leapYear(int year)
{
  if (year < 1000)                  return 2;
  if (year > 9999)                  return 2;
  if (year%4)                       return 0; // if it is NOT a multiple of 4, it is NOT a leap year
  if (year%100 == 0 && year%400!=0) return 0; // if it IS a multiple of 100 and NOT a multiple of 400 it is NOT a leap year
  return 1;                                   // otherwise it IS a leap year
}
You can take as an example the following code that I have written just now.:)

At least it works.

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
43
44
45
46
47
48
49
50
51
52
using System;

namespace LeapYear
{
    class Program
    {
        static bool IsLeapYear(int year)
        {
            return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        }

        static void Main(string[] args)
        {
            string yesNo;

            do
            {
                string entry;
                int year = 0;
                bool valid;

                do
                {
                    Console.Write("\nEnter a four-digits year: ");
                    entry = Console.ReadLine();

                    valid = int.TryParse(entry, out year) && 
                            year > 0                      && 
                            year.ToString().Length == 4;

                    if ( !valid ) Console.WriteLine( "Error: shall be a four-digits number." + 
                                                     "Try again." );
                } while (!valid);

                Console.WriteLine();

                if (IsLeapYear(year))
                {
                    Console.WriteLine("{0} is a leap year.", year);
                }
                else
                {
                    Console.WriteLine("{0} is not a leap year.", year);
                }

                Console.Write("\nWould you like to continue? (y/n ): ");
                yesNo = Console.ReadLine();
                yesNo = yesNo.ToUpper();
            } while (yesNo == "Y");
        }
    }
}
Last edited on
closed account (3TXyhbRD)
Use code tags and format you code. Thank you.
Your solution was on track, just a few minor errors

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
#include <iostream> 
using namespace std; 
int leapyear (int yr)
{
  if ((yr % 4 == 0) && !(yr % 100 == 0))
    return 1;	
  else if (yr % 400 == 0) 
    return 1;
  return 0;
  
}

int main()
{
  const int arraySize = 4;
  
  int year[ arraySize ]; 
  
  cout << "Enter " << arraySize << " four digits years:\n";
  
  for ( int i = 0; i < arraySize; i++ )
  {
    cin >> year[ i ];
    if (year[i] > 9999 || year[i] < 1000)
    {
      cout <<"That was an incorrect year, make sure it is only 4 digits long" <<endl;
      --i;
    }
  }
  cout <<endl;
  
  for (int r = 0; r < arraySize; ++r)
    if (leapyear(year[r]))
      cout << year[r] <<" is a leap year.\n"; 
  return 0;
}



$ ./File
Enter 4 four digits years:
999
That was an incorrect year, make sure it is only 4 digits long
2348
2010
10000
That was an incorrect year, make sure it is only 4 digits long
4000
7675

2348 is a leap year.
4000 is a leap year.
Smac89 (49)
1
2
3
4
5
6
7
8
9
  for ( int i = 0; i < arraySize; i++ )
  {
    cin >> year[ i ];
    if (year[i] > 9999 || year[i] < 1000)
    {
      cout <<"That was an incorrect year, make sure it is only 4 digits long" <<endl;
      --i;
    }
  }


I think that in the original post the array was defined that each its element would contain only one digit. It was not defined to contain four years.:)
It is a bad idea to remove original post! Now it is not clear what code is being discussed.
Last edited on
closed account (3TXyhbRD)
I removed the original post via Report. Someone edited to contain only nonsense.
Last edited on
Topic archived. No new replies allowed.