Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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-Jun-23 20:26pm

1 solution

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.
 
Share this answer
 

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



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