📜  使用三元运算符查找最大数的程序

📅  最后修改于: 2021-05-28 03:54:12             🧑  作者: Mango

任务是编写一个程序,以使用以下三元运算符查找最大数:

  • 两个数字
  • 三个数字
  • 四个数字

例子

Input : 10, 20 
Output : 
Largest number between two numbers (10, 20) is: 20

Input : 25 75 55 15
Output : 
Largest number among four numbers (25, 75, 55, 15) is: 75

三元运算符的格式如下:

exp1 ? exp2 : exp3

表达式exp1将始终被求值。 exp2exp3的执行取决于exp1的结果。如果exp1的结果不为零,则将评估exp2 ,否则将评估exp3

  • 程序使用三元运算符查找两个数字中的最大数字。
C++
// C++ program to find largest among
// two numbers using ternary operator
#include 
using namespace std;
 
int main()
{
     
    // Variable declaration
    int n1 = 5, n2 = 10, max;
     
    // Largest among n1 and n2
    max = (n1 > n2) ? n1 : n2;
     
    // Print the largest number
    cout << "Largest number between "
         << n1 << " and " << n2
         << " is " << max << ".";
   
    return 0;
}
 
// This code is contributed by khushboogoyal499


C
// C program to find largest among two
// numbers using ternary operator
 
#include 
 
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, max;
     
    // Largest among n1 and n2
    max = (n1 > n2) ? n1 : n2;
     
    // Print the largest number
    printf("Largest number between %d and %d is %d. ",
                                         n1, n2, max);
 
    return 0;
}


Java
// Java program to find largest
// among two numbers using
// ternary operator
class GFG
{
public static void main(String args[])
{
    // variable declaration
    int n1 = 5, n2 = 10, max;
     
    // Largest among n1 and n2
    max = (n1 > n2) ? n1 : n2;
     
    // Print the largest number
    System.out.println("Largest number between " + n1 +
                  " and " + n2 + " is " + max + ". " );
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 program to find largest
# between two numbers using
# Ternary Operator.
 
# As python doesn't have ternary operator,
# we use conditional expression,
# form of conditional expression:
# exp1 if condition else exp2
# if condition is true then exp1
# is evaluated otherwise exp2
# max is library function to find
# the maximum number, so can't use
# max as variable
n1 = 5
n2 = 10
 
mx = n1 if n1 > n2 else n2
 
# we also can use
# mx=(n1>n2) and n1 or n2
 
# str() function is used to
# convert integer to string,
# it is required as we are
# concatenating integers with string
print("Largest number between " + str(n1) +
                        " and " + str(n2) +
                        " is " + str(mx))
 
# This code is contributed by Santanu


C#
// C# program to find largest
// among two numbers using
// ternary operator using System;
using System;
 
class GFG
{
public static void Main()
{
    // variable declaration
    int n1 = 5, n2 = 10, max;
     
    // Largest among n1 and n2
    max = (n1 > n2) ? n1 : n2;
     
    // Print the largest number
    Console.WriteLine("Largest number between " +
                     n1 + " and " + n2 + " is " +
                                    max + ". " );
}
}
 
// This code is contributed
// by Subhadeep


PHP
 $n2) ? $n1 : $n2;
 
// Print the largest number
echo "Largest number between " .
     $n1. " and " .$n2 ." is " . $max;
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Javascript


C++
// C++ program to find largest among three
// numbers using ternary operator
#include 
using namespace std;
 
int main()
{
     
    // Variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1, n2 and n3
    max = (n1 > n2) ?
          (n1 > n3 ? n1 : n3) :
          (n2 > n3 ? n2 : n3);
           
    // Print the largest number
    cout << "Largest number among "
         << n1 << ", " << n2 << " and "
         << n3 << " is " << max << "." ;
          
    return 0;
}
 
// This code is contributed by khushboogoyal499


C
// C program to find largest among three
// numbers using ternary operator
 
#include 
 
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1, n2 and n3
    max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
     
    // Print the largest number
    printf("Largest number among %d, %d and %d is %d.",
                                         n1, n2, n3, max);
 
    return 0;
}


Java
// Java program to find largest
// among three numbers using
// ternary operator
class GFG
{
public static void main(String args[])
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1, n2 and n3
    max = (n1 > n2) ?
          (n1 > n3 ? n1 : n3) :
          (n2 > n3 ? n2 : n3);
     
    // Print the largest number
    System.out.println("Largest number among " + n1 +
                             ", " + n2 + " and " + n3 +
                                 " is " + max + ". " );
}
}
 
// This code is contributed
// by Arnab Kundu


Python3
# Python3 program to find largest
# among three numbers using
# ternary operator
 
# As python doesn't have ternary
# operator, we use conditional
# expression, in form of
# conditional expression:
# Here, mx is a variable
n1 = 5
n2 = 10
n3 = 15
 
mx = (n1 if (n1 > n2 and n1 > n3) else
     (n2 if (n2 > n1 and n2 > n3) else n3))
 
# We also can use
# mx=(n1>n2) and n1 or n2
 
# str() function is used to
# convert integer to string,
# it is required as we are
# concatenating integers with string
print("Largest number among " + str(n1) +
                        " , " + str(n2) +
                      " and " + str(n3) +
                       " is " + str(mx))
 
# This code is contributed by Santanu


C#
// C# program to find largest
// number among three numbers
// using ternary operator
using System;
 
class GFG
{
public static void Main()
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1 and n2
    max = (n1 > n2 && n1 > n2) ?
                n1 : (n2 > n3) ? n2 : n3;
     
    // Print the largest number
    Console.WriteLine("Largest number among " +
                     n1 + ", " + n2 + " and " +
                            n3 + " is " + max);
}
}
 
// This code is contributed by Santanu


PHP
 $n2) ? ($n1 > $n3 ? $n1 : $n3) :
                     ($n2 > $n3 ? $n2 : $n3);
 
// Print the largest number
echo "Largest number among ". $n1 . ", ". $n2 .
                   " and " . $n3. " is " .$max;
?>


Javascript


C++
// C++ program to find largest
// number among four numbers
// using ternary operator
#include 
using namespace std;
 
int main()
{
     
    // Variable declaration
    int n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
     
    // Largest among n1, n2, n3 and n4
    max = (n1 > n2 && n1 > n3 && n1 > n4) ?
               n1 : ((n2 > n3 && n2 > n4) ?
               n2 : (n3 > n4 ? n3 : n4));
     
    // Print the largest number
    cout << "Largest number among "
         << n1 << ", " << n2 << ", "
         << n3 << " and " << n4
         << " is " << max << "." ;
          
    return 0;
}
 
// This code is contributed by khushboogoyal499


C
// C program to find largest among four
// numbers using ternary operator
 
#include 
 
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
     
    // Largest among n1, n2, n3 and n4
    max = (n1 > n2 && n1 > n3 && n1 > n4) ?
                                   n1 :
                                     ((n2 > n3 && n2 > n4) ?
                                                         n2 :
                                                          (n3 > n4 ? n3 : n4));
     
    // Print the largest number
    printf("Largest number among %d, %d, %d and %d is %d.",
                                         n1, n2, n3, n4, max);
 
    return 0;
}


Java
// Java program to find largest
// among four numbers using
// ternary operator
class GFG
{
 
public static void main(String args[])
{
    // variable declaration
    int n1 = 5, n2 = 10,
        n3 = 15, n4 = 20, max;
     
    // Largest among n1, n2, n3 and n4
    max = (n1 > n2 && n1 > n3 && n1 > n4) ?
               n1 : ((n2 > n3 && n2 > n4) ?
               n2 : (n3 > n4 ? n3 : n4));
     
    // Print the largest number
    System.out.println("Largest number among " +
                      n1 + ", " + n2 + ", "+ n3 +
             " and " + n4 + " is " + max + ". " );
}
}
 
// This code is contributed
// by Arnab Kundu


Python3
# Python3 program to find largest
# among four numbers using
# Ternary Operator.
 
# As python doesn't have ternary operator,
# we use conditional expression,
# form of conditional expression:
# exp1 if condition else exp2
# if condition is true then exp1
# is evaluated otherwise exp2
# max is library function to find
# the maximum number, so can't use
# max as variable
n1 = 5
n2 = 10
n3 = 15
n4 = 20
 
mx = (n1 if (n1 > n2 and n1 > n2 and n1 > n4)
         else (n2 if (n2 > n3 and n3 > n4)
         else (n3 if n3 > n4 else n4)))
 
# str() function is used to convert
# integer to string, it is required
# as we are concatenating integers
# with string
print("Largest number among " + str(n1) + ", " +
            str(n2) + ", " + str(n3) + " and " +
            str(n4) + " is " + str(mx))
 
# This code is contributed by Santanu


C#
// C# program to find largest
// number among four numbers
// using ternary operator
using System;
 
class GFG
{
public static void Main()
{
    // variable declaration
    int n1 = 5, n2 = 10,
        n3 = 15, n4 = 20, max;
     
    // Largest among n1 and n2
    max = (n1 > n2 && n1 > n2 && n1 > n2) ?
                n1 : (n2 > n3 && n2 > n4) ?
                           n2 : (n3 > n4) ? n3 : n4;
     
    // Print the largest number
    Console.WriteLine("Largest number among " +
                        n1 + ", " + n2 + ", " +
                            n3 + " and " + n4 +
                                 " is " + max);
}
}
 
// This code is contributed by Santanu


PHP
 $n2 && $n1 > $n3 && $n1 > $n4) ?
             $n1 : (($n2 > $n3 && $n2 > $n4) ?
             $n2 : ($n3 > $n4 ? $n3 : $n4));
 
// Print the largest number
echo "Largest number among ". $n1. ", ". $n2. ", ".
                  $n3.  " and ". $n4. " is " .$max;
?>


Javascript


输出:
Largest number between 5 and 10 is 10.
  • 程序使用三进制运算符在三个数字中查找最大的数字。

C++

// C++ program to find largest among three
// numbers using ternary operator
#include 
using namespace std;
 
int main()
{
     
    // Variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1, n2 and n3
    max = (n1 > n2) ?
          (n1 > n3 ? n1 : n3) :
          (n2 > n3 ? n2 : n3);
           
    // Print the largest number
    cout << "Largest number among "
         << n1 << ", " << n2 << " and "
         << n3 << " is " << max << "." ;
          
    return 0;
}
 
// This code is contributed by khushboogoyal499

C

// C program to find largest among three
// numbers using ternary operator
 
#include 
 
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1, n2 and n3
    max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
     
    // Print the largest number
    printf("Largest number among %d, %d and %d is %d.",
                                         n1, n2, n3, max);
 
    return 0;
}

Java

// Java program to find largest
// among three numbers using
// ternary operator
class GFG
{
public static void main(String args[])
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1, n2 and n3
    max = (n1 > n2) ?
          (n1 > n3 ? n1 : n3) :
          (n2 > n3 ? n2 : n3);
     
    // Print the largest number
    System.out.println("Largest number among " + n1 +
                             ", " + n2 + " and " + n3 +
                                 " is " + max + ". " );
}
}
 
// This code is contributed
// by Arnab Kundu

Python3

# Python3 program to find largest
# among three numbers using
# ternary operator
 
# As python doesn't have ternary
# operator, we use conditional
# expression, in form of
# conditional expression:
# Here, mx is a variable
n1 = 5
n2 = 10
n3 = 15
 
mx = (n1 if (n1 > n2 and n1 > n3) else
     (n2 if (n2 > n1 and n2 > n3) else n3))
 
# We also can use
# mx=(n1>n2) and n1 or n2
 
# str() function is used to
# convert integer to string,
# it is required as we are
# concatenating integers with string
print("Largest number among " + str(n1) +
                        " , " + str(n2) +
                      " and " + str(n3) +
                       " is " + str(mx))
 
# This code is contributed by Santanu

C#

// C# program to find largest
// number among three numbers
// using ternary operator
using System;
 
class GFG
{
public static void Main()
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, max;
     
    // Largest among n1 and n2
    max = (n1 > n2 && n1 > n2) ?
                n1 : (n2 > n3) ? n2 : n3;
     
    // Print the largest number
    Console.WriteLine("Largest number among " +
                     n1 + ", " + n2 + " and " +
                            n3 + " is " + max);
}
}
 
// This code is contributed by Santanu

的PHP

 $n2) ? ($n1 > $n3 ? $n1 : $n3) :
                     ($n2 > $n3 ? $n2 : $n3);
 
// Print the largest number
echo "Largest number among ". $n1 . ", ". $n2 .
                   " and " . $n3. " is " .$max;
?>

Java脚本


输出:
Largest number among 5, 10 and 15 is 15.
  • 程序使用三进制运算符在四个数字中查找最大的数字。

C++

// C++ program to find largest
// number among four numbers
// using ternary operator
#include 
using namespace std;
 
int main()
{
     
    // Variable declaration
    int n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
     
    // Largest among n1, n2, n3 and n4
    max = (n1 > n2 && n1 > n3 && n1 > n4) ?
               n1 : ((n2 > n3 && n2 > n4) ?
               n2 : (n3 > n4 ? n3 : n4));
     
    // Print the largest number
    cout << "Largest number among "
         << n1 << ", " << n2 << ", "
         << n3 << " and " << n4
         << " is " << max << "." ;
          
    return 0;
}
 
// This code is contributed by khushboogoyal499

C

// C program to find largest among four
// numbers using ternary operator
 
#include 
 
int main()
{
    // variable declaration
    int n1 = 5, n2 = 10, n3 = 15, n4 = 20, max;
     
    // Largest among n1, n2, n3 and n4
    max = (n1 > n2 && n1 > n3 && n1 > n4) ?
                                   n1 :
                                     ((n2 > n3 && n2 > n4) ?
                                                         n2 :
                                                          (n3 > n4 ? n3 : n4));
     
    // Print the largest number
    printf("Largest number among %d, %d, %d and %d is %d.",
                                         n1, n2, n3, n4, max);
 
    return 0;
}

Java

// Java program to find largest
// among four numbers using
// ternary operator
class GFG
{
 
public static void main(String args[])
{
    // variable declaration
    int n1 = 5, n2 = 10,
        n3 = 15, n4 = 20, max;
     
    // Largest among n1, n2, n3 and n4
    max = (n1 > n2 && n1 > n3 && n1 > n4) ?
               n1 : ((n2 > n3 && n2 > n4) ?
               n2 : (n3 > n4 ? n3 : n4));
     
    // Print the largest number
    System.out.println("Largest number among " +
                      n1 + ", " + n2 + ", "+ n3 +
             " and " + n4 + " is " + max + ". " );
}
}
 
// This code is contributed
// by Arnab Kundu

Python3

# Python3 program to find largest
# among four numbers using
# Ternary Operator.
 
# As python doesn't have ternary operator,
# we use conditional expression,
# form of conditional expression:
# exp1 if condition else exp2
# if condition is true then exp1
# is evaluated otherwise exp2
# max is library function to find
# the maximum number, so can't use
# max as variable
n1 = 5
n2 = 10
n3 = 15
n4 = 20
 
mx = (n1 if (n1 > n2 and n1 > n2 and n1 > n4)
         else (n2 if (n2 > n3 and n3 > n4)
         else (n3 if n3 > n4 else n4)))
 
# str() function is used to convert
# integer to string, it is required
# as we are concatenating integers
# with string
print("Largest number among " + str(n1) + ", " +
            str(n2) + ", " + str(n3) + " and " +
            str(n4) + " is " + str(mx))
 
# This code is contributed by Santanu

C#

// C# program to find largest
// number among four numbers
// using ternary operator
using System;
 
class GFG
{
public static void Main()
{
    // variable declaration
    int n1 = 5, n2 = 10,
        n3 = 15, n4 = 20, max;
     
    // Largest among n1 and n2
    max = (n1 > n2 && n1 > n2 && n1 > n2) ?
                n1 : (n2 > n3 && n2 > n4) ?
                           n2 : (n3 > n4) ? n3 : n4;
     
    // Print the largest number
    Console.WriteLine("Largest number among " +
                        n1 + ", " + n2 + ", " +
                            n3 + " and " + n4 +
                                 " is " + max);
}
}
 
// This code is contributed by Santanu

的PHP

 $n2 && $n1 > $n3 && $n1 > $n4) ?
             $n1 : (($n2 > $n3 && $n2 > $n4) ?
             $n2 : ($n3 > $n4 ? $n3 : $n4));
 
// Print the largest number
echo "Largest number among ". $n1. ", ". $n2. ", ".
                  $n3.  " and ". $n4. " is " .$max;
?>

Java脚本


输出:
Largest number among 5, 10, 15 and 20 is 20.

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。