Q:

C Program To Find Smallest Number Using Conditional Operator

0

you have to make this program in the following way:

  • C Program To Find Smallest of Two Numbers Using Conditional Operator
  • C Program To Find Smallest of Three Numbers Using Conditional Operator
  • C Program To Find Smallest of Four Numbers Using Conditional Operator
  • C Program To Find Smallest of Five Numbers Using Conditional Operator

All Answers

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

C Program To Find Smallest of Two Numbers Using Conditional 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 Smallest among two numbers 
//using Conditional operator or ternary operator

#include<stdio.h>

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

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

  // Smallest among a and b
   small = a<b?a:b;

  //Display smallest number
   printf("Smallest among 2 Number is : %d",small);

}

Output -:

Enter two number
12
17

Smallest among 2 Number is : 12

C Program To Find Smallest of Three Numbers Using Conditional Operator

Program -:

//C program to find Smallest among three numbers 
//using Conditional operator or ternary operator

#include<stdio.h>

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

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

  // Small among a, b and c
   small = a<b?a<c?a:c:b<c?b:c;

  //Display smallest number
   printf("Smallest Among 3 Number is : %d",small);

}

Output -:

Enter Three Number
12
10
17

Smallest Among 3 Number is : 10

C Program To Find Smallest of Four Numbers Using Conditional Operator

Program -:

//C program to find Smallest among four numbers using Conditional or ternary operator

#include<stdio.h>

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

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

 // Smallest among a, b, c and d2
   small = ( (a<b && a<c && a<d) ? a : (b<c && b<d) ? b : (c<d)? c : d );

 //Display Smallest number
   printf("Smallest Number is : %d",small);

}

Output -:

Enter Four Number
12
4
23
6

Smallest Number is : 4

C Program To Find Smallest of five Numbers Using Conditional Operator

Program -:

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

#include<stdio.h>

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

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

 // Smallest among a, b, c and d
   small = ( (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 Smallest number
   printf("Smallest Number is : %d",small);

}

Output:

Enter five number
12
4
32
5
3

Smallest Numbers is : 3

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 The Largest Number Using Conditi... >>