I need help with a programming question?

Home » Programming & Design » I need help with a programming question?
Programming & Design No Comments

Get the user to enter 3 numbers, get the program to find the highest number and show it. Without while loops

Other answer:

Andy T:
Very easy:
1. let the 3 numbers be x y z
2. tentatively set max_num = x
3. if (y > x) set max_num = y
4. if (z > max_num) set max_num = z

There you have it, and consider switching course of study in college. Oh just a thought, few months down the line you'll be required to do the exact same no-loop exercise but with variable quantity of numbers, it is nice little brain-twister for a freshman.

Andrew:
In what programming language? C,Java,Python or any other? for C/C++ it would be store the 3 numbers in an array then in a for loop compare the numbers with a if statement then use another variable to swap one out so it is not overwritten the put it back in the correct place within the loop
Chris P:
10 DIM A(3)
20 FOR N=1 TO 3
30 INPUT A(N)
40 NEXT N
50 LET H=-999999
60 FOR N=1 TO 3
70 IF A(N)>H THEN LET H=A(N)
80 NEXT N
90 PRINT H
Newms34:
Assuming the teacher doesn't want you to use a pre-baked solution like JS's Math.max() function, it'd be something like this:
var numOne = parseInt(prompt('First number?'));
var numTwo = parseInt(prompt('Second number?'));
var numThree = parseInt(prompt('Third number?'));
//note that the above may be a bad idea: You're not doing any error checking (i.e., if the user enters 'apple', which cannot be converted to a number, what does the program do?). However, I doubt your teacher needs this.
//now just do the rest with ifs:
if(numOne>numTwo && numOne>numThree){
alert(numOne);
}else if(numTwo>numOne && numTwo>numThree){

etc.
The rest is just basically a repeat of the test for 1.

Cheries:
It's easy, you can use If function to check the greatest value
Here an example using VBA programming language.

Option Explicit
'//Method to get user input, find the greatest number and show it to user
Public Sub GetHighestDemo()
Dim a As Long, b As Long, c As Long

'get user input
a = InputBox("Enter number a")
b = InputBox("Enter number b")
c = InputBox("Enter number c")

'find the highest number
Dim highest As Long
highest = GetHighest(a, b, c)

'show the highest number using message box
MsgBox highest
End Sub
'//Function to get the highest value
Private Function GetHighest(a As Long, b As Long, c As Long) As Long
If (a > b And a > c) Then GetHighest = a 'check if a bigger than b and c, if true return a
If (b > a And b > c) Then GetHighest = b 'check if b bigger than a and c, if true return b
If (c > a And c > b) Then GetHighest = a 'check if c bigger than a and b, if true return c
End Function

EddieJ:
You should start with a simpler assignment.

Get the user to enter TWO numbers and get the program to find the highest number and show it.

Can you do that? If so, then show us, or show us your best attempt.

lolita:
Var num1 : int
Var num2 : int
Var num3 : int
Put "enter 1st number"
Get num1
Put "enter 2nd number"
Get num2
Put "enter 3rd number"
Get num3

If num1 > num2 and num1>num3 then
Put "the highest number is " , num1
Elseif num2>num1 and num2 > num3 then
Put "the highest number is " , num2
Else
Put "the highest number is " , num3

I think it should be something like this, I learned basic programming 2 years ago so if my answer is not correct sorry !!

md:
input first
assign to highest
get second number
if greater than highest assign to highest
get third number
if greater than highest assign to highest
print highest
roger:
input first
assign to highest
get second number
if greater than highest assign to highest
get third number
if greater than highest assign to highest
print highest
Michael:
Simple example in Java:

int max;
int i;
max = in.nextInt();
i = in.nextInt();
if(i > max) {
max = i;
}
i = in.nextInt();
if(i > max) {
max = i;
}

System.out.println(max);

LEAVE A COMMENT