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”
int n1, n2, n3;
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) )
else if(n2 > n3)
printf(“\n\n\t THE SMALlEST NMBER IS…: “);
if( (n1 < n2) && (n1 < n3) )
else if(n2 < n3)
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
Copyright©2012 electrofriends.com All Rights Reserved