Step by step descriptive logic to find maximum between three numbers.
1 Input three numbers from user. Store it in some variable say num1, num2 and num3.
2. Compare first two numbers i.e. num1 > num2. If the statement is true then num2 is surely not max value. Perform one more comparison between num1 with num3 i.e. if(num1 > num3), then num1 is max otherwise num3.
3. If the statement num1>num2 is false. Which indicates that num1 is not max. Hence, this time compare num2 with num3. If the statement num2>num3 is true then num2 is max otherwise num3.
/**
* C program to find maximum between three numbers using nested if
*/
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
/* If num1 > num2 and num1 > num3 */
max = num1;
}
else
{
/* If num1 > num2 but num1 > num3 is not true */
max = num3;
}
}
else
{
if(num2 > num3)
{
/* If num1 is not > num2 and num2 > num3 */
max = num2;
}
else
{
/* If num1 is not > num2 and num2 > num3 */
max = num3;
}
}
/* Print maximum value */
printf("Maximum among all three numbers = %d", max);
return 0;
}
Hello guys in this program if num1 = 3 num2 =2 and num3 = 1.Then the program first checks 3>2 if that satisfies then it checks 3>1 if this is not satisfies then max = num3;that means num3 =1 ids the greatest number.which is not possible.I am confused which line will be executed first