Algorithm Analysis
We find the maximum among four numbers using a conditional statement.
Algorithm Implementation
Read the input data.
scanf("%d %d %d %d", &a, &b, &c, &d);
Assume the maximum is equal to the first number.
max = a;
If one of the following numbers is greater than the maximum, then we recalculate the answer.
if (b > max) max = b; if (c > max) max = c; if (d > max) max = d;
Output the answer.
printf("%d\n", max);
Java Implementation
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); int a = con.nextInt(); int b = con.nextInt(); int c = con.nextInt(); int d = con.nextInt(); int res = a; if (b > res) res = b; if (c > res) res = c; if (d > res) res = d; System.out.println(res); con.close(); } }
Python Implementation
a, b, c, d = map(int, input().split()) print(max(a, b, c, d))