Search code examples
cleap-year

Find the next leap year in C


I'm having trouble with an exercise on my homework. I have to make a function that tells me when the next leap year is given an n (or n if it's a leap year).

I already tackled the last part, but I'm having trouble with the "next leap year" part. I assume I have to do a cycle?

Here's what I have so far

int next_leapyear(int n) 
{

    if(((n%4==0)&&(n%100!=0))||(n%400==0)) return n;

    else
    while(?){
      n++;
     }

     return n;
}

I'm just starting to learn this language, so if you guys could keep it simple I would appreciate it


Solution

  • Increase n until your if statement condition is not true so there are 2 ways either you can put condition in while braces, which is straight forward.

    2nd is that, run loop infinite times and put breaking condition inside the loop

    while(1){
          n++;
    if(((n%4==0)&&(n%100!=0))||(n%400==0)) break;
    
         }