Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Leap year algorithm

Leap year algorithm

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marca292
    wrote on last edited by
    #1

    Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?

    bool isLeapYear(int year)
    {
    bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
    return leapYear;
    }

    Thanks!

    J CPalliniC S S 4 Replies Last reply
    0
    • M marca292

      Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?

      bool isLeapYear(int year)
      {
      bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
      return leapYear;
      }

      Thanks!

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      It's the only algorithm I know. It's so simple that asking for another one makes no sense. To optimize for speed you may define it as macro or inline function (better):

      #define IS_LEAP_YEAR(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))

      inline bool isLeapYear(int year) const
      {
      return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
      }

      1 Reply Last reply
      0
      • M marca292

        Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?

        bool isLeapYear(int year)
        {
        bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
        return leapYear;
        }

        Thanks!

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        First code has to be syntactically correct, then fast.

        Veni, vidi, vici.

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • M marca292

          Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?

          bool isLeapYear(int year)
          {
          bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
          return leapYear;
          }

          Thanks!

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #4

          Does this work for you Leap Year[^]?

          1 Reply Last reply
          0
          • CPalliniC CPallini

            First code has to be syntactically correct, then fast.

            Veni, vidi, vici.

            S Offline
            S Offline
            SandipG
            wrote on last edited by
            #5

            Did you forget about this Leap Year[^] code? :)

            CPalliniC 1 Reply Last reply
            0
            • S SandipG

              Did you forget about this Leap Year[^] code? :)

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              How did you find that! :-D

              Veni, vidi, vici.

              In testa che avete, signor di Ceprano?

              S 1 Reply Last reply
              0
              • CPalliniC CPallini

                How did you find that! :-D

                Veni, vidi, vici.

                S Offline
                S Offline
                SandipG
                wrote on last edited by
                #7

                i have it bookmarked :)

                1 Reply Last reply
                0
                • M marca292

                  Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?

                  bool isLeapYear(int year)
                  {
                  bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
                  return leapYear;
                  }

                  Thanks!

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  It's looks good, syntax errors aside (missing a closing bracket).

                  Steve

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups