Program 3
CHECK LARGEST
The function/method largest check which number is largest out of three.
The function/method largest takes 3 integers as input and returns largest interger out of three integers.
The function/method largest compiles successfully but fails to get the desired result for some test cases due to logical errors. Your task is to fix the code so that it passes all the test cases.
Incorrect Code
void largest (int n1, int n2, int n3) { if (n1 >= n2 && n1 <= n3) printf ("%d is the largest number.", n1); else if (n2 <= n1 && n2 >= n3) printf ("%d is the largest number.", n2); else printf ("%d is the largest number.", n3); }
Correct Code
void largest (int n1, int n2, int n3) { if (n1 >= n2 && n1 >= n3) printf ("%d is the largest number.", n1); else if (n2 >= n1 && n2 >= n3) printf ("%d is the largest number.", n2); else printf ("%d is the largest number.", n3); }
Login/Signup to comment