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
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
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?