15,668,842 members
1.00/5 (1 vote)
See more:
```static int userInputSum(){
Scanner sc = new Scanner(System.in);
int sum = 0;
int num;

do{
System.out.println("Enter the number: ");
num = sc.nextInt();
System.out.println("Enter 0 to end the loop");
sum += num;
}while (num > 0);

System.out.println("Sum is "+ sum);
return -1;
}
```

What I have tried:

i have got to sum the user input , but pls can u help me with how to print the largest number input by user.
Posted
Updated 22-Nov-22 0:20am

## Solution 1

Set a variable to zero. Then as you read each number, compare it with that variable. If it is greater replace the variable with the number. Once you have read all the numbers the variable will contain the largest value.

## Solution 2

import java.util.Scanner;
// problem Take integer inputs till the user enters 0 and print the largest number from all.

public class Problem4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int variable = 0;
while(true)
{
int m = input.nextInt();
if( m == 0 )
{
break;
}
if( m>variable)
{
variable=m;
}

}
System.out.println(variable);
}
}

## Solution 3

```public class program
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, a=0;
System.out.print("ENTER A NUMBER: ");
num = sc.nextInt();

// Condition Starts
while(num!=0){
System.out.print("ENTER A NUMBER: ");
num = sc.nextInt();
System.out.println("ENTER 0 TO END LOOP");
if(num>a){
a = num; // The value of a will change to the value of num
}
}// Condition Ends
System.out.print("The largest number entered by the user is: "+a)
}
}```

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 237 Graeme_Grant 85 Richard MacCutchan 80 Dave Kreskowiak 58 Richard Deeming 50
 OriginalGriff 1,040 Richard MacCutchan 202 Patrice T 110 Graeme_Grant 100 Richard Deeming 50

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900