This is a Java Program to Return a Value from a Method.
We make a method named input which is used to get input from the user. We also make a method named add which is used to perform addition and return the result back to input method where we finally print the result.
Here is the source code of the Java Program to Return a Value from a Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Return_Value
{
void input()
{
int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print("Enter first integer:");
x = s.nextInt();
System.out.print("Enter second integer:");
y = s.nextInt();
z = add(x, y);
System.out.println("Result:"+z);
}
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
public static void main(String[] args)
{
Return_Value obj = new Return_Value();
obj.input();
}
}
Output:
$ javac Return_Value.java $ java Return_Value Enter first integer:5 Enter second integer:6 Result:11
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Programming Books