to find the biggest and smallest of three numbers

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

Here is an optimized program to find the largest and smallest among three entered numbers.

Logic : The entered three numbers N1, N2, N3 are compared in such an order, where the first comparison checks if  N1  largest. If so, prints it out and exists; if not so, it omits N1 from further comparison and checks with N2 and N3 only. The same logic but with complement operator, the program finds the least among the three.

The program needs to include the standard IO library, “stdio.h”

#include<stdio.h>
void main()
{
int n1, n2, n3;
clrscr();
printf(“\n\n\t ENTER THREE NUMBERS A,B,C…(separated by commas): “);
scanf(“%d,%d,%d”, &n1, &n2, &n3);
printf(“\n\n\t THE BIGGEST NUMBER IS…: “);
if( (n1 > n2) && (n1 > n3) )
printf(“%d”, n1);
else if(n2 > n3)
printf(“%d”, n2);
else
printf(“%d”, n3);
printf(“\n\n\t THE SMALlEST NMBER IS…: “);
if( (n1 < n2) && (n1 < n3) )
printf(“%d”, n1);
else if(n2 < n3)
printf(“%d”, n2);
else
printf(“%d”,n3);
getch();
}

Download exe and source code here.

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

Leave a Reply