- To Create a function to add 2 numbers
User Input: 2,3
Output: 5
import java.util.Scanner;
public class SumNummber {
public static int sum(int n1,int n2) {
return n1+n2;
}
public static void main(String []args) {
int n1,n2;
Scanner userInput = new Scanner(System.in);
System.out.print("Enter two numbers : ");
n1 = userInput.nextInt();
n2 = userInput.nextInt();
System.out.print("Sum : "+sum(n1,n2));
}
}
Enter two numbers : 7 5 Sum : 12Program
//function to add 2 numbers
public static int sum(int n1,int n2){
return n1+n2; //use add(+) operator
}
Since, function returns integer data type, so we declare int
just before the function name
For devision (/), subtraction (-), multiplication (*), you can use their respective operators
ConceptComing Soon !
QuickStrength does not come from winning. Your struggles develop your strengths.