how can I find minium or macimum of more than 2 numbers in Java using math methods please
I can tell the logic and C and C++ implementations ..
tell me but i have to use import java.math and i'm not allowed to use "if" anyway tell me so i can see if i can find something useful
I don't know if it works in java but I remember that once we used max(max(a,b),max(c,d)) for max of 4 numbers in C. Are "n" numbers given like 3,4? or should it work in any way?
Here's an example, hope it helps. int[] arr = {-1, 4, 6, -6, 10, 100}; int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for(int num : arr) { min = Math.min(num, min); max = Math.max(num, max); } System.out.printf("Min: %d\nMax: %d\n", min, max);
ndani14 is right. And note that you don't need to use java.math for this at all. Math is in the java.lang package, which is automatically imported for you, so you don't even need to import it. sinans' approach will work, too, if you know exactly how many numbers you'll have.
thank you all of you @ndani thanks but the task is that i'm not allowed to use "for" or "if" @sinans hope it helps
@sinans it worked thank you thank you
Put them in array... keep the [0] index maximum number. Check in a loop if the number is greater than previous one.. You can do same thing for minimum
Aside from posting your question 8 times, you've given us very little to go on. I know some other requirements for your problem because I read your other 7 posts, but someone who just saw *this* question wouldn't know that you need to find min, max, and average of 4 numbers supplied by the user (I still don't know how the user is supposed to supply them). AFTER people have posted answers you've revealed that: 1. You can't use Integer.parseInt() 2. You can't use "if" 3. You can't use "for" The straightforward way to solve your general problem is to use these things. But apparently they want you to use some certain library functions that are not obvious. Why don't you just post the entire problem, with all of its requirements and caveats, instead of making people guess what it is? You also keep insisting that you need to use java.math, which, from the requirements you've posted, you have absolutely no actual need for. If there is some other hidden requirement that the classes in that package would be useful for, please post it.
thank you for your time but i solved the problem because sinans gave me help and yes i should not use those because i am beginner at Java and I just know few things anyway thank you and sorry that i'm impatient i just get a little nervous when i don\t knoe hoe to solve an exercise
Join our real-time social learning platform and learn together with your friends!