- 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
6 Respuestas
+ 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 .
+ 2
Please show your code so we can help you!
+ 2
thanks
+ 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.
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);
}
}
}
0
can you please help