3

I tried several times how to write a algorithm and a pseudo code for a program of finding the largest value among 3 user input integers? . I couldn't make it properly. Can i be helped?

4
  • what you tried..add something. Commented Jul 31, 2018 at 5:22
  • @Alien i assumed that user inputs are x,y,z then if x>y then again checked the condition x>z if it is true, x is the largest. While x>y , z>y and then there is an anther condition if z>x then z is the largest number. Like wise i tried. It's hard to check conditions likewise. Commented Jul 31, 2018 at 5:31
  • Edit your question and add own pseudocode - seems you realize what to do but doubt in details. Commented Jul 31, 2018 at 8:48
  • Why do you think that finding the largest out of three integers is different than finding the largest out of, say, eleven integers? Commented Jul 31, 2018 at 9:01

2 Answers 2

2

Pseudocode for maximum of 3 integers-

print max(max(first_integer,second_integer),third_integer)
Sign up to request clarification or add additional context in comments.

5 Comments

I think i didn't get this. Not clear enough to understand to me
What was hard though?
i didn't get what happens with this code. max(max(first_integer, second_integer), third_integer)
max(a,b) finds maximum between a and b
Aha. . I got it now. Thankz
2

So you have three numbers, x, y, and z. You want the largest one. So here are some rules:

  1. If x > y, then the largest can't be y; it must be x or z. So check to see if x > z.
  2. If x < y, then the largest can't be x; it must be y or z. So check to see if y > z.

That results in the code:

if (x > y)
    if (x > z)
        largest = x;
    else
        largest = z;
else // y >= x
    if (y > z)
        largest = y;
    else
        largest = z;

If you have a max function that returns the maximum of two numbers, then you can simplify that code:

largest = max(x, y);
largest = max(largest, z);

Which can further be optimized to largest = max(max(x, y), z);

1 Comment

Ok. Thankz. it has been explained well. i will try to think like this way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.