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 = 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 multiple of 400 and
- if N is multiple of 4 and not multiple of 100
If both of the above conditions are true, then N is a leap year, else not
Program:
C
// C program to check if N is a leap year // using command line arguments #include <stdio.h> #include <stdlib.h> /* 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; } |
chevron_right
filter_none
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."); } } |
chevron_right
filter_none
Output:
Recommended Posts:
- TCS Coding Practice Question | Checking Prime Number
- TCS Coding Practice Question | HCF or GCD of 2 Numbers
- TCS Coding Practice Question | Check Odd or Even
- TCS Coding Practice Question | LCM of 2 Numbers
- TCS Coding Practice Question | Fibonacci Series
- TCS Coding Practice Question | Reverse a Number
- TCS Coding Practice Question | Concatenate 2 Strings
- TCS Coding Practice Question | Average of 2 Numbers
- TCS Coding Practice Question | Sum of Digits of a number
- TCS Coding Practice Question | Factorial of a Number
- TCS Coding Practice Question | Swap two Numbers
- TCS Coding Practice Question | Greatest of 3 Numbers
- TCS Coding Practice Question | Palindrome String
- TCS Coding Practice Question | Reverse a String
- TCS Coding Practice Question | Prime Numbers upto N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.






