How do I solve this exercise?

Started by
1 comment, last by jbadams 5 years, 5 months ago

Write a program that asks the user to enter a series of non-zero integers, one at a time.
The program should keep asking the user for a number until they enter a value of 0.
The program should then display the largest of the numbers that have been entered.
Be aware that the user could enter all negative integers and your program has to be
able to handle this.

What I have tried:

I've been at if for a couple of hours, just decided i hit a wall and can't do it. I've been taking programming courses for 3 weeks so I have no idea if this is hard or not but it looks like I can't solve it. I know for a fact I have to use loops and I've tried making a loops that compares the last number the user inputs to the previous one and keeping the largest then repeating until all numbers have been checked. I still think that's the solution but I don't know how to write it. Thanks in advance.

Advertisement

This sounds like homework, so I'm not going to give you any code, but..

The loop should be about collecting user input, and it should probably be endless.  The zero input should Break from the loop somehow.  If you only need the largest value, then you only need to keep track of that.  You'll need to use some IF statements.   And you'll need to output the largest number you were keeping track of after exiting or right before exiting the loop.  You'll probably also have to convert some things from strings to numbers of some kind. ;)

Good luck!

I'm sorry, but we have a policy of not answering homework questions here. If we give you the answer you won't learn from the exercise as intended.

Septopus has given you some hints that might help you in the right direction. If you're having trouble, you could try drawing a flow chart or something out some pseudocode to figure out the logic before trying to write your program.

Good luck! :)

 

19 minutes ago, fleabay said:

Go away

No need to be rude, this is a welcoming community. Please report topics for moderation if you feel they don't belong, rather than telling someone to "go away".

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement