Maximum among four numbers#7812

Maximum among four numbers (Editorial)

The maximum among four numbers can be found using a conditional statement.

Algorithm implementation

Read the input data.

scanf("%d %d %d %d",&a,&b,&c,&d);

Initialize the maximum value with the first number.

max = a;

Sequentially compare the remaining numbers with the current maximum. If any of the numbers is greater than the maximum, update the maximum value.

if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;

Print 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

Read the input data.

a, b, c, d = map(int,input().split())

Print the maximum of four numbers.

print(max(a,b,c,d))