📜  TCS编码实践问题|检查Le年

📅  最后修改于: 2021-05-24 16:43:13             🧑  作者: Mango

给定数字N,任务是使用命令行参数检查N是否为is年。

例子:

Input: N = 2000
Output: Yes

Input: N = 1997
Output: No

方法:

  • 由于该数字是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入数字
  • 提取的数字将为String类型。
  • 将此数字转换为整数类型并将其存储在变量中,例如N
  • 现在检查以下情况:
    • 如果N是400的倍数并且
    • 如果N是4的倍数而不是100的倍数

    如果以上两个条件都成立,则N为a年,否则为

程序:

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.");
    }
}


输出:

  • 在C中:
  • 在Java:
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”