Click here to Skip to main content
15,668,842 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-Nov-22 0:20am

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
 
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);
}
}
 
Share this answer
 
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)
    }
}
 
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