Saturday, 2 August 2014

Find Greatest Number

Find Greatest Number
Find Greatest Number from three numbers Using Ternary Operator

Ternary operator

If any operator is used on three operands or variable is known as ternary operator. It can be represented with " ?: "
Ternary Operator
In the above symbol exp1 is condition and exp2 and exp3 will be either value or variable or statement or any mathematical expression. If condition will be true exp2 will be executer otherwise exp3 will be executed.
Example 1:
int a =7, b=20, c;
C=a>b;a:b;
Value of (c)  -----> 20
Example 2:
C=a< b ? a + b : a - b
Value of (c) -----> 27
Find largest number among 3 numbers
int a=15, b=21, c=-13, Large;
Large=(a>b)?(a>c?a:c):(b>c?b:c)
Value of (Large)  -----> 21
largest number
/*****************************************************
    Find Greatest Number
******************************************************/
#include< stdio.h>
#include< conio.h>
void main()
 {
 int a,b,c,largest;
 clrscr();
 printf("Enter any Three Numbers: ");
 scanf("%d%d%d",&a,&b,&c);
 largest=(a>b)?(a>c?a:c):(b>c?b:c);
 printf("Largest Values: %d",largest);
 getch();
}
Output
images

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...