1
0

How to return minimum of four numbers?

Task: Write a function that returns a minimum of four numbers. The function min(a,b,c,d) has to use (call) the function min(a, b)

I cannot get how to write it? I tried to compare first two numbers and then second two numbers, but it didn’t pass testing.

asked 10 Feb '15, 08:59

Diego%20Carleone's gravatar image

Diego Carleone ♦♦
7616
accept rate: 83%

public class Solution { public static int min(int a, int b, int c) { if(a<b && b<c){ return a; } if(b<a && b<c){ return b; }else{ return c; } }

      public static void main(String[] args) throws Exception
{
    System.out.println( min(1,2,3) );
    System.out.println( min(-1,-2,-3) );
    System.out.println( min(3,5,3) );
    System.out.println( min(10,5,10) );
}

}

(03 Apr '15, 00:43) Abraiz Khan

Method min(a,b) should compare and return the minimum of two passed arguments. In method min(a, b, c, d) use method min(a, b), call it three times (first you compare a and b, c and d, and then compare their results) and return the minimum.

link

answered 10 Feb '15, 08:59

Diego%20Carleone's gravatar image

Diego Carleone ♦♦
7616
accept rate: 83%

//package com.javarush.test.level02.lesson08.task03;

/ Minimum of three numbers Write a function that returns a minimum of three numbers. / public class Solution { public static int min(int a, int b, int c) { if(a<b && b<c){ return a; } if(b<a && b<c){ return b; }else{ return c; } }

public static void main(String[] args) throws Exception
{
    System.out.println( min(1,2,3) );
    System.out.println( min(-1,-2,-3) );
    System.out.println( min(3,5,3) );
    System.out.println( min(10,5,10) );
}

}

link

answered 03 Apr '15, 00:42

Abraiz%20Khan's gravatar image

Abraiz Khan
11
accept rate: 0%

package com.javarush.test.level02.lesson08.task04;

/ Minimum of four numbers Write a function that returns a minimum of four numbers. The function min(a,b,c,d) has to use (call) the function min(a, b) / public class Solution { public static int min(int a, int b, int c, int d) { int k=min(a,b); int j=min(c,d); int l=min(k,j); return l; }

public static int min(int a, int b)
{ 
    if(a<b)
    return a;
    else
    return b;
}

public static void main(String[] args) throws Exception
{
    System.out.println( min(-20, -10) );
    System.out.println( min(-20, -10, -30, -40) );
    System.out.println( min(-20, -10, -30, 40) );
}

}

link

answered 17 Apr '15, 17:42

Muhammad%20Abu%20Bakr%20Chaudhry's gravatar image

Muhammad Abu...
12
accept rate: 0%

public static int min(int a, int b){ if (a<b && a<c && a<d){ return a; if (b<a && b<c && b<d){ return b; if (c<a && c<b && c<d){ return c; else { return d; } } } } } public static int min(int a, int b) { if(a<b) return a; else return b; }

link

answered 17 Apr '15, 20:30

rahul's gravatar image

rahul
7916
accept rate: 15%

public static int min(int a, int b, int c, int d) {

      if (b < a) {
      a = b;
  }
  if (c < a) {
      a = c;
  }
  if (d < a) {
      a = d;
  }
  return b;
}
public static int min(int a, int b)
{
    int n;
    if(a<b)
    n=a;
    else
    n=b;
    return n;
}
public static void main(String[] args) throws Exception
{
    System.out.println( min(-20, -10) );
    System.out.println( min(-20, -10, -30, -40) );
    System.out.println( min(-20, -10, -30, 40) );
}

}

link

answered 21 Mar '17, 11:58

xnopino's gravatar image

xnopino
112
accept rate: 0%

public class Solution { public static int min(int a, int b, int c, int d) { return min(min(a,b),min(c,d)); }

public static int min(int a, int b)
{
    if (a > b)
        a = b;
    else
    return a;
}

public static void main(String[] args) throws Exception
{
    System.out.println( min(-20, -10) );
    System.out.println( min(-20, -10, -30, -40) );
    System.out.println( min(-20, -10, -30, 40) );
}

}

link

answered 26 Oct '17, 08:45

Sviatoslav's gravatar image

Sviatoslav
1
accept rate: 0%

  1. public static int min(int a, int b, int c, int d)
  2. {
  3. if (a<=b && a<=c && a<=d) return a;
  4. if (b<a && b<c && b<d) return b;
  5. if (c<a && c<b && c<d) return c;
  6. else return d;
  7. }
  8. public static int min(int a, int b)
  9. {
  10. if (a<=b) return a;
  11. else return b;
  12. }
link

answered 01 Nov '17, 13:25

Winnermann's gravatar image

Winnermann
111
accept rate: 0%

public static int min(int a, int b, int c, int d)
{
int m = min(a,b);
if (m<c && m<d)
return m;
else

if (c<d)
return c;
else
return d;
    }

public static int min(int a, int b)
{
int m2;
if (a<b)
m2=a;
else
m2=b;
return m2;

}
link

answered 29 Jun, 08:20

McLAKOV's gravatar image

McLAKOV
11
accept rate: 0%

edited 29 Jun, 08:26

{ public static int min(int a, int b, int c, int d)

{

    if (min(a,b)<=c && min(a,b)<d)

    return min(a,b);

    else if (min(a,b)<=min(a,b) && c<=d)

    return c;

    else

    return d;

}

public static int min(int a, int b)

{

    if (a<=b)

    return a;

    else

    return b;

}

public static void main(String[] args) throws Exception
{
    System.out.println( min(-20, -10) );
    System.out.println( min(-20, -10, -30, -40) );
    System.out.println( min(-20, -10, -30, 40) );
}

}

link

answered 06 Sep, 06:40

Kris's gravatar image

Kris
11
accept rate: 0%

edited 06 Sep, 06:47

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×69
×53
×17

Asked: 10 Feb '15, 08:59

Seen: 4,358 times

Last updated: 06 Sep, 06:47