Given a number N, the task is to check if N is a Leap Year or not, using Command Line Arguments. Examples:
Input: N = 2000
Output: Yes
Input: N = 1997
Output: No
Approach:
- Since the number is entered as Command line Argument, there is no need for a dedicated input line
- Extract the input number from the command line argument
- This extracted number will be in string type.
- Convert this number into integer type and store it in a variable, say N
- Now check for the below conditions:
- if N is a multiple of 400 and
- if N is multiple of 4 and not multiple of 100
Program:
C
#include <stdio.h>
#include <stdlib.h> /* atoi */
int isLeapYear( int year)
{
if (((year % 4 == 0)
&& (year % 100 != 0))
|| (year % 400 == 0))
return 1;
else
return 0;
}
int main( int argc, char * argv[])
{
int n;
if (argc == 1)
printf ( "No command line arguments found.\n" );
else {
n = atoi (argv[1]);
if (isLeapYear(n) == 1)
printf ( "Yes\n" );
else
printf ( "No\n" );
}
return 0;
}
|
Java
class GFG {
public static int isLeapYear( int year)
{
if (((year % 4 == 0 )
&& (year % 100 != 0 ))
|| (year % 400 == 0 ))
return 1 ;
else
return 0 ;
}
public static void main(String[] args)
{
if (args.length > 0 ) {
int n = Integer.parseInt(args[ 0 ]);
if (isLeapYear(n) == 1 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
else
System.out.println( "No command line "
+ "arguments found." );
}
}
|
Python3
class GFG:
@staticmethod
def isLeapYear(year):
if ((year % 4 = = 0 and year % 100 ! = 0 ) or year % 400 = = 0 ):
return 1
else :
return 0
if __name__ = = "__main__" :
import sys
if len (sys.argv) > 1 :
n = int (sys.argv[ 1 ])
if GFG.isLeapYear(n) = = 1 :
print ( "Yes" )
else :
print ( "No" )
else :
print ( "No command line arguments found\nUse : python3 GFG.py <number>" )
|
Output:
- In C:

- In Java:

- In Python

Time Complexity: O(1)
Auxiliary Space: O(1)