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
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
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
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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
View Comments
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?