Program to find largest of three numbers using ternary operator in C
- Home
- Find elements
- Program to find largest of three numbers using ternary operator in C
- On
- By
- 0 Comment
- Categories: Find elements, Operators
Program to find largest of three numbers using ternary operator in C
Program to find largest of three numbers using ternary operator in C
In this program, we will discuss a simple concept of the Program to find largest of three numbers using ternary operator in C
In this topic, we are going learn how to find the biggest number from given three numbers using ternary operator in C programming language.
Using ternary operator to find the largest
Using ternary operator to find the largest in one line
In this program, we will find the largest number out of given three number using ternary operator in C language
Program 1
#include <stdio.h> #include <stdlib.h> //int biggestNum(int a, int b,int c); int main() { int num1,num2,num3; //declare the variables printf("Please Enter three numbers: "); //Ask input from the user scanf("%d %d %d",&num1,&num2,&num3); //Reading the input from user and store the variables int result=num3>(num1>num2?num1:num2)?num3:((num1>num2)? num1:num2); //find largest using ternary operator printf("Largwst number is: %d",result); //Display result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Please Enter three numbers: 34 78 61 Largest number is: 78
Using ternary operator to find the largest in two line
Program 2
In this program, we will find the largest number out of given three number using ternary operator in C language
#include <stdio.h> #include <stdlib.h> //int biggestNum(int a, int b,int c); int main() { int num1,num2,num3; //declare the variables printf("Enter three numbers: "); //Ask input from the user scanf("%d %d %d",&num1,&num2,&num3); //Reading the input from user and store the variables int temp=(num1>num2)? num1:num2; //Compare num1 and num2 using ternary operator int largest= num3>temp?num3:temp; //Compare num3 and temp variable using ternary operator printf("Largest number is: %d",largest); //Display the result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Please Enter three numbers: 87 99 45 Largest number is: 99
In the above, all programs, Three variables num1,num2,num3 are compared one by one using ternary operator to find largest one.
Suggested for you
Nested if statements in C language
Operator in C language
Similar post
largest number of three number in C
largest number of three number in C++
largest number of three number in Python
largest number of three number in C using function
largest number of three number in C++ using function
largest number of three number in Python using function
largest number of three number in Java using method
Smallest number of three numbers in Java language
Smallest number of three numbers in C language
Smallest number of three numbers in C++ language
Smallest number of three numbers in Python language
Smallest number of three numbers using method in Java language
Smallest number of three numbers using function in C language
Smallest number of three numbers using function in C++ language
Smallest number of three numbers using function in Python language