Basic while loop program

while loop syntax in c
while loop example java
while loop example c++
do while loop in c example program
while loop questions in c
while loop example python
for loop in c programming example
while loop in c programming example ppt

I've been self-teaching Python for the last month. I have an interview for a coding course and need help on writing a program which uses a while loop. The task is as follows:

Write a program that asks the user to type in 5 numbers, and that outputs the largest of these numbers and the smallest of these numbers. So, for example, if the user types in the numbers 2456 457 13 999 35, the output would be:

The largest number is 2456 The smallest number is 13

Thanks for your help.

Here you go

numbers = [] #this will be the list in which we will store the numbers
while len(numbers) < 5:  #len return the length of your list, we want our while loop to repeat 5 times
    numbers.append(double(input("enter number: "))) # adds the inputed number to the list
print("The largest number is",max(numbers),"The smallest number is",min(numbers))

C - while loop in C programming with example, In while loop, condition is evaluated first and if it returns true then the statements inside while Simple while loop example Check out these related programs:. Basic do while Loop Program. Every loop consists of three parts in sequence: Initialization: Use to initialize the loop variable. Condition: It is checked after each iteration as an entry point to the loop. Updation: Incrementing the loop variable to eventually terminate the loop not satisfying the loop condition.

Next time you ask questions, please include at least some of your progress so that everyone is willing to help you. When you learn a language, a good coding style is also important. Here is an answer for you. I hope you can learn something. You can also try using max() and min() with a list.

input_times = 5
max_num, min_num = -float('inf'), float('inf')
while input_times:
    input_times -= 1
    try:
        num = int(input('Please enter a number:\n'))
        if num < min_num:
            min_num = num
        if num > max_num:
            max_num = num
    except:
        print("Please input an integer!")
        input_times += 1
print('The largest number is {max_num} The smallest number is {min_num}'.format(
       max_num = max_num, min_num = min_num))

While loop in Java with examples, while loop in C programming with the help of examples. In programming, loops are used to repeat a block of code until a specified condition is met. You can place any number of Exit While statements anywhere in the While loop. When used within nested While loops, Exit While transfers control out of the innermost loop and into the next higher level of nesting. The Continue While statement immediately transfers control to the next iteration of the loop. For more information, see Continue Statement. Example. In the following example, the statements in the loop continue to run until the index variable is greater than 10.

def max_min(): 
    my_list = []
    for i in range(1, 6):
        temp_val = input("Enter number {}: ".format(i))
        my_list.append(int(temp_val))
    max_val = max(my_list)
    min_val = min(my_list)
    return "max value is {} and min value is {}".format(max_val, min_val)


print(max_min())

C while and dowhile Loop, A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number​  Guess the output of this while loop. #include <stdio.h> int main() { int var=1; while (var <=2) { printf("%d ", var); } } The program is an example of infinite while loop. Since the value of the variable var is same (there is no ++ or – operator used on this variable, inside the body of loop) the condition var<=2 will be true forever and the loop would never terminate.

Programming - While Loop, construct consists of a process symbol and a condition. In Java, a while loop is used to execute statement(s) until a condition is true. In this tutorial, we learn to use it with examples. First of all, let's discuss its syntax: while (condition(s)) {// Body of loop} 1. If the condition(s) holds, then the body of the loop is executed after the execution of the loop body condition is tested again.

Do while loop, The While programming language is a simple programming language constructed from assignments, sequential  In order to store the sum of the numbers, we declare a variable sum and initialize it to the value of 0. The while loop continues until the user enters a negative number. During each iteration, the number entered by the user is added to the sum variable. When the user enters a negative number, the loop terminates.

C, With the while loop we can execute a set of statements as long as a condition is With the else statement we can run a block of code once when the condition  The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1. The break Statement With the break statement we can stop the loop even if the while condition is true:

Comments
  • What are you progress so far?
  • Possible duplicate of Write a program that asks the user to type in 5 numbers , and that outputs the largest of these numbers and the smallest of these numbers
  • @KristopherJohnson: Not quite a dupe... that other question shows effort from the OP where as this one is a "do it all for me" question... close though, but try harder next time! ;)
  • this is for an interview, you may want to stop interviewing until you have a better grasp of the language? make an attempt at least.
  • Thank you for your help.
  • Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!