📜  TCS编码实践问题| 3个号码中最大的一个

📅  最后修改于: 2021-05-31 23:53:30             🧑  作者: Mango

给定三个数字,任务是使用命令行参数找到三个数字中的最大值。

例子

Input: A = 2, B = 8, C = 1
Output: 8

Input: A = 231, B = 4751, C = 75821
Output: 75821

方法:

  • 由于数字是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入数字
  • 提取的数字将为String类型。
  • 将这些数字转换为整数类型并将其存储在变量中,例如A,B和C
  • 找到最大的数字,如下所示:
    • 检查A是否大于B。
      • 如果为true,则检查A是否大于C。
        • 如果为true,则将“ A”打印为最大数字。
        • 如果为假,则将“ C”打印为最大数字。
      • 如果为假,则检查B是否大于C。
        • 如果为true,则将’B’打印为最大数字。
        • 如果为假,则将“ C”打印为最大数字。
  • 打印或返回最大的数字

程序:

C
// C program to compute the greatest of three numbers
// using command line arguments
  
#include 
#include  /* atoi */
  
// Function to compute the greatest of three numbers
int greatest(int A, int B, int C)
{
  
    if (A >= B && A >= C)
        return A;
  
    if (B >= A && B >= C)
        return B;
  
    return C;
}
  
// Driver code
int main(int argc, char* argv[])
{
  
    int num1, num2, num3;
  
    // 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)"
        num1 = atoi(argv[1]);
        num2 = atoi(argv[2]);
        num3 = atoi(argv[3]);
  
        // Find the greatest and print it
        printf("%d\n", greatest(num1, num2, num3));
    }
    return 0;
}


Java
// Java program to compute the greatest of three numbers
// using command line arguments
  
class GFG {
  
    // Function to compute the greatest of three numbers
    static int greatest(int A, int B, int C)
    {
  
        if (A >= B && A >= C)
            return A;
  
        if (B >= A && B >= C)
            return B;
  
        return C;
    }
  
    // 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 num1 = Integer.parseInt(args[0]);
            int num2 = Integer.parseInt(args[1]);
            int num3 = Integer.parseInt(args[2]);
  
            // Find the greatest
            int res = greatest(num1, num2, num3);
  
            // Print the greatest
            System.out.println(res);
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}


输出:

  • 在C中:

  • 在Java:

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