PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on If & Else

Program to check it is leap year or not

Input - Output

Input
Enter a year : 2021

Output
2021 is not a leap year

Algorithm

Step 1: START

Step 2: Enter a year.

Step 3: Check the condition:

    If year % 4 = 0 and year %100! =0 or

    if year % 4 =0 and year % 400 = 0 then

   print year is a leap year otherwise

   print year is not a leap year

Step 4: END

CODE


/* Check entered year is leap year or not */

#include <stdio.h>
int main()
{
      int year;
      printf(" Enter a year: ");
      scanf("%d",&year);
      if(year%4 == 0)
          if(year%100 == 0)
              if(year%400==0)
                  printf(" %d is a leap year ",year);
              else
                  printf(" %d is not a leap year ",year);
          else
            printf(" %d is a leap year ",year);
      else
         printf(" %d is not a leap year ",year);
    return 0;
}


/* Check entered year is leap year or not */
#include <iostream>
using namespace std;
int main()
{
      int year;
      cout<<" Enter a year: ";
      cin>>year;
      if(year%4 == 0)
          if(year%100 == 0)
              if(year%400==0)
                  cout<<year<<" is a leap year ";
              else
                  cout<<" "<<year<<" is not a leap year ";
          else
            cout<<year<<" is a leap year ";
      else
         cout<<year<<" is not a leap year ";
    return 0;
}

OutPut

Enter a year : 2021

2021 is not a leap year