Question

Write a program that accepts numbers entered by the user until the user enters a zero....

Write a program that accepts numbers entered by the user until the user enters a
zero. The program then must display the largest value that is smaller than zero.
This should be java program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The question can be solved with this code :

(Note : If no value less than zero is given in input , 0 will be printed )

import java.util.*;
import java.lang.*;
import java.io.*;
public class zero {

   public static void main(String[] args) {
      
       //scanner object to recieve input
       Scanner scanner = new Scanner(System.in);


       //initially set ans to 0
       double ans = 0.0;

       //variable to store input
       double value;

       //recieve first input
       value = scanner.nextDouble();

       //loop till input is not 0
       while(value != 0.0) {

           //if input value is negative
           if(value < 0) {

               //if ans is 0.0 means this is first negative value
               if(ans == 0.0)
                   //set ans as this value
                   ans = value;
               //else
               else {
                   // if this negative number is greater than previous largest negative number
                   if (value > ans)
                       //set this negative number as answer
                       ans = value;
               }
           }
           //recieve the next input
           value = scanner.nextDouble();
       }
       //print the answer
       System.out.println(ans);
   }
}

In case of any doubt please comment. Happy Learning :)

Add a comment
Know the answer?
Add Answer to:
Write a program that accepts numbers entered by the user until the user enters a zero....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT