Q:

C Program To Find The Largest Number Using Conditional Operator or Ternary Operator

0

you have to make this program in the following way:

  • C Program To Find The Largest of Two Numbers Using Conditional or Ternary Operator
  • C Program To Find The Largest of Three Numbers Using Conditional or Ternary Operator
  • C Program To Find The Largest of Four Numbers Using Conditional or Ternary Operator
  • C Program To Find The Largest of Five Numbers Using Conditional or Ternary Operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program To Find The Largest of Two Numbers Using Ternary Operator

Algorithm

  1. Program Start
  2. Variable Declaration
  3. Input two number
  4. Check the condition, ternary (conditional) operator.
  5. Display answer according the condition
  6. Program End

Program

//C program to find largest among two numbers using ternary operator

#include<stdio.h>

void main()
{ 
  // Variable declaration
   int a,b,larg;    

   printf("Enter two number\n");
   scanf("%d %d",&a,&b);

 // Largest among a and b
   larg = a>b?a:b; 
     
 //Display largest number
   printf("Largest Number is : %d",larg); 

}

Output

Enter two number
20

18
Largest Number is : 20

C Program To Find The Largest of Three Numbers Using Ternary Operator

Program

//C program to find largest among three numbers using ternary operator

#include<stdio.h>

void main()
{ 
  // Variable declaration
   int a,b,c,larg;    

   printf("Enter three number\n");
   scanf("%d %d %d",&a,&b, &c);

 // Largest among a, b and c
   larg = a>b?a>c?a:c:b>c?b:c; 
     
 //Display largest number
   printf("Largest Number is : %d",larg); 

}

Output

Enter Three Numbers
12
10
11

Largest Number is : 12

C Program To Find The Largest of Four Numbers Using Ternary Operator

Program

//C program to find largest among four numbers using ternary operator

#include<stdio.h>

void main()
{ 
  // Variable declaration
   int a,b,c,d,larg;    

   printf("Enter four number\n");
   scanf("%d %d %d %d",&a,&b, &c, &d);

 // Largest among a, b, c and d
   larg = ( (a>b && a>c && a>d) ? a : (b>c && b>d) ? b : (c>d)? c : d );
     
 //Display largest number
   printf("Largest Number is : %d",larg); 

}

Output

Enter Four Numbers
10
30
20
40

Largest Number is : 40

C Program To Find The Largest of Five Numbers Using Ternary Operator

Program

//C program to find largest among five numbers using ternary operator

#include<stdio.h>

void main()
{
  // Variable declaration
   int a,b,c,d,e,larg;

   printf("Enter five number\n");
   scanf("%d %d %d %d %d",&a,&b, &c, &d, &e);

 // Largest among a, b, c and d
   larg = ( (a>b && a>c && a>d && a>e) ? a : (b>c && b>d && b>e) ? b : (c>d && c>e)? c : (d>e)? d : e );

 //Display largest number
   printf("Largest Number is : %d",larg);

}

Output

Enter Five Numer
10
20
32
28
18
Largest Number is : 32

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C Program To Find Smallest Number Using Conditiona...