Q:

Leap Year Program In C (5 Different Ways)

belongs to collection: Basic C Programming Examples

0

you have to make this program in the following way:

  • C Program to Check Leap Year using if else
  • Write A C Program to Check leap years between the range of two years
  • C Program to Check Leap Year using function
  • C Program to Check Leap Year using conditional operator
  • C Program to Check Leap Year using switch case

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Write A Leap Year Program in C

Algorithm

  1. Program Start
  2. Declaration of variable
  3. Enter year to check leap year or not
  4. Check condition
  5. Give answer according to condition
  6. Program End

Program

//Write A C Program to Check Leap Year Using if else 

#include<stdio.h>
void main()
{
   int year;
   printf("Enter The Year\n");
   scanf("%d",&year);

  if(year%100==0)
   {   if(year%400==0)
        printf("Leap year");
       else
        printf("Not a Leap year");
   }
  else
   { if(year%4==0)
       printf("Leap year");
    else
        printf("Not a Leap year");
   }
}

Output

Enter The Year 
2019
Not a Leap year

Write A C Program to Check Leap Years Between The Range of Two Years

Program

//Write A C Program to Check Leap Years Between The Range of Two Years

#include<stdio.h>
void main()
{
    int startYear, endYear, i;
    printf ("Enter starting years: ");
    scanf ("%d", &startYear);

    printf ("Enter ending years: ");
    scanf ("%d", &endYear);
    printf ("Leap Years between %d to %d are: \n", startYear, endYear);

    for (i= startYear; i<=endYear; i++)
    {
      if(((i% 4== 0) && (i % 100 != 0)) || (i% 400 == 0))
         {
          printf("%d \n",i);
         }
    }
}

Output

Enter starting years: 2000
Enter ending years: 2022
Leap Years between 2000 to 2022 are:
2000
2004
2008
2012
2016
2020

C Program to Check Leap Year Using Function

Program

//C Program to Check Leap Year using function

#include<stdio.h>
void leapyear(int year)
{
    if(year%100==0)
   {   if(year%400==0)
        printf("Leap year");
       else
        printf("Not a Leap year");
   }
  else
   { if(year%4==0)
       printf("Leap year");
    else
        printf("Not a Leap year");
   }
}
void main()
{
   int year;
   printf("Enter The Year\n");
   scanf("%d",&year);

  //Calling Function to Check Leap Year
   leapyear(year);

}

Output

Enter The Year
2020
Leap Year

C Program to Check Leap Year Using Conditional Operator

Program

//C Program to Check Leap Year Using Conditional Operator

#include<stdio.h>
void main()
{
   int year;
   
   printf("Enter the Year :\n");
   scanf("%d",&year);

 (year%100==0)?(year%400==0)? printf("Leap year"): printf("Not a Leap year") :(year%4==0)? printf("Leap year"): printf("Not a Leap year");

}

Output

Enter the Year
2022
Not a Leap Year

C Program to Check Leap Year Using Switch case

Program

//C Program to Check Leap Year Using Switch Case Statement

#include<stdio.h>
void main()
{
   int year,y;

   printf("Enter the Year :\n");
   scanf("%d",&year);

 y = year % 400 == 0 || year % 100 == 0 || year % 4 == 0;

 switch (y) {
        case 1:
            printf("\n%d is the leap year.\n", year);
            break;

        case 0:
            printf("\n%d is not the leap year.\n", year);
            break;

        default:
            printf("\n%d is not the leap year.\n", year);
    }

}

Output

Enter the Year
2020

2020 is the leap year

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Odd or Even Program In C (7 Different Ways)... >>
<< C Program To Find Area and Circumference of Circle...