Answer:
Here is code in java.
import java.util.*;
class Main
{
// main method of the class
public static void main (String[] args) throws java.lang.Exception
{
try{
// print the running total of all inputs
System.out.println("total of all the inputs is: "+fun());
}catch(Exception ex){
return;}
}
/* function to take input from user until input is -1
and keep a running total */
public static int fun()
{
int in;
//scanner class object to read the input
Scanner scr=new Scanner(System.in);
// variable to keep running total
int total=0;
System.out.println("please enter an Integer:(-1 to stop)");
// read the input first time
in=scr.nextInt();
while(in!=-1)
{
// this loop keep taking input until input is -1.
// calculate running total of all inputs
total=total+in;
System.out.println("please enter an number:(-1 to stop)::");
in=scr.nextInt();
}
// return total of all inputs
return total;
}
}
Explanation:
Create an object of scanner class in the method fun_tot(),it will take input from user.In while loop it will ask user to give input and calculate the sum of all inputs.when user enter -1 then while loop breaks and the method will return the total of all inputs given by the user.
Output:
please enter an number:(-1 to stop)::23
please enter an number:(-1 to stop)::3
please enter an number:(-1 to stop)::5
please enter an number:(-1 to stop)::76
please enter an number:(-1 to stop)::12
please enter an number:(-1 to stop)::-1
total of all the inputs is: 119