- On
- By
- 1 Comment
- Categories: addition, Calculations
Addition of two numbers in Java using method
addition of two numbers in Java using methods
In this tutorial, we will discuss the addition of two numbers in Java using the Java method.
In this topic, we will learn a simple concept of how to add two number in Java programming language using the Java method.
already we will know the same concept using the operator in a simple way.
if you known click here Java program to sum of two numbers
This program has following procedures to completion
- class and main method
- get input from user – using Scanner class
- method definition – user-define method
- calling the method
- Display method on the screen
Calculate the total value of the two integer number
Calculate the total value of two integer number using the method
import java.util.Scanner; public class SumOfNum{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); // create scanner object System.out.print("Enter the first number: "); int num1=sc.nextInt(); //this method reads value for num1 providing by user System.out.print("Enter the second number: "); int num2=sc.nextInt(); //this method reads value for num2 providing by user sc.close();//closing the scanner class sumNum(num1,num2); //calling the method } //sum method public static void sumNum(int num_A,int num_B){ int sum=0; sum=num_A+num_B; System.out.println("Sum of two numbers "+sum); } }
When the above code is compiled and executed, it produces the following results
Enter the first number: 45 Enter the second number: 32 Sum of two numbers 77
Program 2
Calculate total value of two integer number using the method with the return value
import java.util.Scanner; public class SumOfNumbur{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); // create scanner object System.out.print("Enter the first number: "); int num1=sc.nextInt(); //this method reads value for num1 providing by user System.out.print("Enter the second number: "); int num2=sc.nextInt(); //this method reads value for num2 providing by user sc.close();//closing the scanner class int Total=calcTotal(num1,num2); //calling the method System.out.println("Sum of two numbers "+Total); } //calcTotal method public static int calcTotal(int x,int y){ int result=x+y; return result;//return result to main } }
When the above code is compiled and executed, it produces the following results
Enter the first number: 55 Enter the second number: 65 Sum of two numbers 120
In this program, we can denoted some impotent steps to completed this program
- class and main method
- get input from user – using Scanner class
- method definition – user-define method
- calling the method
- return value to main method
Calculate total value of two floating point number
Calculate the total value of the two floating point number using the method
import java.util.Scanner; public class SumOfNum1{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); // create scanner object System.out.print("Enter the first number: "); double num1=sc.nextDouble(); //this method reads value for num1 providing by user System.out.print("Enter the second number: "); double num2=sc.nextDouble(); //this method reads value for num2 providing by user sc.close();//closing the scanner class calcTotal(num1,num2); //calling the method } //calcTotal method public static void calcTotal(double num_A,double num_B){ double result; result=num_A+num_B; System.out.println("Sum of two numbers "+result); } }
When the above code is compiled and executed, it produces the following results
Enter the first number: 34.23 Enter the second number: 32.54 Sum of two numbers 66.77
Similar post
Addition of two numbers in Java
Addition of two numbers in C++
Addition of two numbers in Python
Addition of two numbers in PHP
Addition of two numbers in JavaScript
Sum of two numbers in Java using method
Sum of two numbers in C++ using function
Sum of two numbers in Python using function
C program to add two numbers without using arithmetic operators
C++ program to add two numbers without using arithmetic operators
Python program to add two numbers without using arithmetic operators
Suggested for you
1 Comment
Fla Cis August 1, 2019 at 8:40 am
I want to add 3 double numbers for example : 4.67, 8.93, 10.00. if result is natural number return 1 else return 0. what is the condition? can you help me?