- 4

WAP to find and print greatest numbers from different numbers entered by user.The program should terminate when a zero is enter

in java

18th Sep 2021, 4:53 PM
Huzaifa Inaya
Huzaifa Inaya - avatar
6 ответов
+ 2
Huzaifa Inaya As in your code you taking input and storing in n variable its fine after that you have decleared two variables which have default values zero . And in if condition you applying condition with default values and your loop will be execute n times running unnecessarily. You need to set any values to a and b variable. Or you can take user inputs if u want to check for multiple input values then you need to write both input statements inside loop . Hope you got your mistakes. Note: Before compiling your code always do dry run that a good practice .
18th Sep 2021, 7:23 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Please show your code so we can help you!
18th Sep 2021, 4:54 PM
Lisa
Lisa - avatar
+ 2
thanks
18th Sep 2021, 5:13 PM
Huzaifa Inaya
Huzaifa Inaya - avatar
+ 1
According to the task description you need multiple input. You could get input in a while loop and store the numbers in a list. When the user enters 0, you break from the loop. Then you create a variable that will hold the max number (give it a dummy value, like 0 for example) and iterate through the list: if the current value is greater than the value of your "max" variable, reassign the current value to max.
18th Sep 2021, 5:12 PM
Lisa
Lisa - avatar
0
import java.util.Scanner; class Largest { public static void main() { Scanner sc=new Scanner(System. in); System.out.println("enter a number"); int n=sc.nextInt(); int a,b; for (int i=1;n>=0;i++) if (a>b) { System. out.println(a); } else if(a<b) { System. out.println(b); } } }
18th Sep 2021, 5:03 PM
Huzaifa Inaya
Huzaifa Inaya - avatar
0
can you please help
18th Sep 2021, 5:07 PM
Huzaifa Inaya
Huzaifa Inaya - avatar