64.4k views
4 votes
Write a program that finds the largest in a series of numbers entered by the user.The program must prompt the user to enter numbers one by one.When the user enters 0 or a negative number, the program must display the largest nonnegative number entered:Enter a number:60Enter a number:38.3Enter a number:4.89Enter a number:100.62Enter a number:75.2295Enter a number: 0The largest number entered was 100.62.

2 Answers

2 votes

Final answer:

The question asks for a program that finds the largest nonnegative number entered by a user, which stops taking input upon entry of 0 or a negative number. A Python example provided demonstrates this functionality using a while loop and conditional statements.

Step-by-step explanation:

The student is asking how to create a program that continuously prompts the user to enter numbers and determines the largest nonnegative number entered. When the user enters 0 or a negative number, the program should display the largest number input before that point. Below is a simple example in Python:

max_number = float('-inf')
while True:
number = float(input('Enter a number: '))
if number <= 0:
break
if number > max_number:
max_number = number
if max_number == float('-inf'):
print('No positive numbers were entered.')
else:
print('The largest number entered was', max_number)

This script uses a while loop to ask the user for inputs and updates the max_number variable whenever a larger number is found. The loop breaks when a non-positive number is entered, and the program then displays the largest number found.

User Hyperboreus
by
6.5k points
0 votes

Answer:

Here is c program:

#include<stdio.h>

#include<conio.h>

void main()

{

//Variable declaration

//array to hold 6 values you can change this as needed

float nums[6];

//integer i is for loop variable

int i;

//to hold maximum value

float max=0.0;

//clear screen

clrscr();

//iterate 6 times you can change as per your need

for(i=0;i<6;i++)

{

printf("Enter a number:");

scanf("%f",&nums[i]);

//check if entered value is greater than previous value

//if it is greater then assign it

if(nums[i]>max)

max = nums[i];

}

//print the value

printf("The largest number entered was %f",max);

getch();

}

Step-by-step explanation:

User Damian
by
5.6k points