C program Biggest of 3 number

The following two C program finds biggest of given 3 numbers read from the user using if conditional statement and ternary operator. The input 3 numbers read from user as inputs and finds biggest number from the inputs.


IF condition and ternary operator

if-else ladder

       if ( <condition-1> ) {
             <statements 1 >
        } 
      else if ( <condtion-2>) {
             <statements 2 >
        } 
       else {
           <statements 3 >
       }
   
  • if-else ladder has successive one or more if and if-else conditional statements,
  • if any one of condition satisfied in if-else ladder, control executes following statements enclosed by curly brace { <statement>} and then control come out of if-else ladder.
  • if a condition is not satisfied in if-else ladder, control goes to next condition else-if (<statement>).
  • if none of condition satisfied in if-else ladder, the control executes else part of statement.

Ternary operator

A ternary operator consists of a ? (question mark) and a : (colon) symbol.
       <condtion > ? <statements-1 > : <statements-2 > ;    
  
  • The ternary operator it executes like a if-else statement, but it is composed of if-else as single line of conditional statement.
  • if condition is satisfied, the following ? (question mark) statement-1 is executed.
  • if condition is not satisfied, the following : (colon) statement-2 is executed.
  • statement-1 and statement-2 must be single-line of statement

Biggest of 3 number - IF conditional C program

The C program finds biggest of given 3 numbers using IF else ladder conditional statements and prints it result as biggest of given numbers.

 
#include <stdio.h>
int main() {

   int a,b,c;         
   printf ("Find a biggest number among 3 numbers");
   
   printf("\n Enter value for a :" );
   scanf("%d", &a);
   printf("\n Enter value for b :" );
   scanf("%d", &b);
   printf("\n Enter value for c :" );
   scanf("%d", &c);
   
   if ( a>b  && a>c )
      printf("\n a=%d is biggest ",a);       
   else if ( b > c )
      printf("\n b=%d is biggest ",b);         
   else 
      printf("\n c=%d is biggest ",c);         
      
 return 0;
}

 

Biggest of 3 number - IF conditional C program Output



 Find a biggest number among 3 numbers

 Enter value for a 23

 Enter value for a 22

 Enter value for a 11

The a =23 is biggest 



Biggest of 3 number - ternary Operator C program

The C program finds biggest of given 3 numbers using ternary operators and prints it result as biggest of given numbers.

 
#include <stdio.h>
int main() {

   int a,b,c;         
   int big;
   printf ("Find a biggest number among 3 numbers");
   
   printf("\n Enter value for a :" );
   scanf("%d", &a);
   printf("\n Enter value for b :" );
   scanf("%d", &b);
   printf("\n Enter value for c :" );
   scanf("%d", &c);
   
    big = (a>b && a>c) ? a : (b>c) ? b : c;
    printf("\n The biggest is %d",big);       
   
      
 return 0;
}
 

Biggest of 3 number - ternary Operator C program output


 Find a biggest number among 3 numbers

 Enter value for a 10

 Enter value for a 12

 Enter value for a 17

The biggest is 17


Download c program source code


Comments

Popular posts from this blog

Hexadecimal to Binary Conversion

Binary to Decimal conversion -Bitwise Left shift Operator

Convert Octal to Binary