Open In App

TCS Coding Practice Question | Checking Leap Year

Given a number N, the task is to check if N is a Leap Year or not, using Command Line Arguments. Examples:

`Input: N = 2000Output: YesInput: N = 1997Output: 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

 `// C program to check if N is a leap year``// using command line arguments` `#include ` `#include /* atoi */` `// Function to check``// if year is a leap year or not``int` `isLeapYear(``int` `year)``{` `    ``// Return 1 if year is a multiple``    ``// 0f 4 and not multiple of 100.``    ``// OR year is multiple of 400.``    ``if` `(((year % 4 == 0)``         ``&& (year % 100 != 0))``        ``|| (year % 400 == 0))``        ``return` `1;``    ``else``        ``return` `0;``}` `// Driver code``int` `main(``int` `argc, ``char``* argv[])``{` `    ``int` `n;` `    ``// Check if the length of args array is 1``    ``if` `(argc == 1)``        ``printf``(``"No command line arguments found.\n"``);``    ``else` `{` `        ``// Get the command line argument and``        ``// Convert it from string type to integer type``        ``// using function "atoi( argument)"``        ``n = ``atoi``(argv[1]);` `        ``// Check if n is a leap year``        ``if` `(isLeapYear(n) == 1)``            ``printf``(``"Yes\n"``);``        ``else``            ``printf``(``"No\n"``);``    ``}` `    ``return` `0;``}`

Java

 `// Java program to check if N is a leap year``// using command line arguments` `class` `GFG {` `    ``// Function to check``    ``// if year is a leap year or not``    ``public` `static` `int` `isLeapYear(``int` `year)``    ``{` `        ``// Return 1 if year is a multiple``        ``// 0f 4 and not multiple of 100.``        ``// OR year is multiple of 400.``        ``if` `(((year % ``4` `== ``0``)``             ``&& (year % ``100` `!= ``0``))``            ``|| (year % ``400` `== ``0``))``            ``return` `1``;``        ``else``            ``return` `0``;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// Check if length of args array is``        ``// greater than 0``        ``if` `(args.length > ``0``) {` `            ``// Get the command line argument and``            ``// Convert it from string type to integer type``            ``int` `n = Integer.parseInt(args[``0``]);` `            ``// Check if n is a leap year``            ``if` `(isLeapYear(n) == ``1``)``                ``System.out.println(``"Yes"``);``            ``else``                ``System.out.println(``"No"``);``        ``}``        ``else``            ``System.out.println(``"No command line "``                               ``+ ``"arguments found."``);``    ``}``}`

Python3

 `#  Python program to check if N is a leap year``#  using command line arguments` `class` `GFG:``    ``# Function to check``    ``# if year is a leap year or not``    ``@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``       ` `        ``# Check if length of args array is``        ``# greater than 1``        ``if` `len``(sys.argv) > ``1``:``        ` `            ``# Get the command line argument and``        ``# Convert it from string type to integer type``            ``n ``=` `int``(sys.argv[``1``])``           ` `            ``# Check if n is a leap year``            ``if` `GFG.isLeapYear(n) ``=``=` `1``:``                ``print``(``"Yes"``)``            ``else``:``                ``print``(``"No"``)``        ``else``:``            ``print``(``"No command line arguments found\nUse : python3 GFG.py "``)` ` ``#puligokulakishorereddy`

Output:

• In C:
• In Java:
• In Python

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