How do you do addition in Java?
In this program, you’ll learn to store and add two integer numbers in Java. After addition, the final sum is displayed on the screen. To sum two number, we will use the mathematical plus (+) symbol between two numbers and assign into another variable. Example: c = a + b; We can perform addition between integer number to an integer number, float number to float number, etc or we can also do integer to float and float to double by type conversion.
Addition of two Integer Numbers
In this program, we will learn to do sum of two integer numbers.
Java Program to Add two Integers Number Algorithm
- import java.io.*;
- import java.util.*;
- Create Class Class_Name
- public static void main(String[] args)
- Scanner ob = new Scanner (System.in);
- System.out.println( “enter value of x and y”);
- int x, y, sum;
- x = ob.nextInt();
- y = ob.nextInt();
- sum = x + y;
- System.out.println( ” Total sum is: “+sum);
Source code
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner ob = new Scanner(System.in);
int x, y;
System.out.println("enter value of x");
x = ob.nextInt();
System.out.println("enter value of y");
y = ob.nextInt();
int sum = x + y;
System.out.println("Total sum is :" +sum);
}
}
Output:
enter value of x
10
enter value of y
20
Total sum is :30
Test Code Here:
Addition of Two Float Numbers in Java
In this program add two float numbers is not more different than addition two integer numbers. When we are writing a decimal value that will be considered as a double value so we have to do type conversion.
example:
a = 123.344 this value is considered as double ( double a = 123.344)
Float a = 123.344, this is a wrong way to declare float in java
Float b = 123.344f; this is considered as float value
Java Addition of two float numbers Algorithm
- import java.io.*;
- import java.util.*;
- Create Class Class_Name
- public static void main(String[] args)
- Scanner ob = new Scanner (System.in);
- System.out.println( “enter value of x and y”);
- Float x, y, sum;
- // Float x = 123.344f;
- // Float y = 121.344f;
- x = ob.nextFloat();
- y = ob.nextFloat();
- sum = x + y;
- System.out.println( ” Total sum is: “+sum);
Source Code:
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner ob = new Scanner(System.in);
Float x, y;
System.out.println("enter value of x");
x = ob.nextFloat();
System.out.println("enter value of y");
y = ob.nextFloat();
Float sum = x + y;
System.out.println("Total sum is :" +sum);
}
}
Output:
enter value of x
123.334
enter value of y
121.344
Total sum is :244.67801
Test Code Here:
Addition of Two Numbers using sum() method
Addition program in java using sum() method user will enter the input two values with nextInt() method. The sum () method will take two arguments and return the addition of both arguments.
Algorithm:
- import java.util.*;
- Create Class Class_Name
- public static void main(String[] args)
- Scanner ob = new Scanner (System.in);
- System.out.println( “enter value of num1 and num2”);
- int num1, num2, num3;
- x = obs.nextInt();
- y = obs.nextInt();
- num3 = Sum(num1, num2);
- System.out.println( ” Total sum is: ” +num3);
- static int Sum(arg1, arg2)
- return art1 + arg2;
Source Code:
import java.util.*;
class Main
{
public static void main(String[] arg)
{
int num1, num2;
Scanner obs=new Scanner(System.in);
System.out.println("Enter first number");
num1 = obs.nextInt();
System.out.println("Enter second number");
num2 = obs.nextInt();
int num3 = Sum(num1,num2);
System.out.println(" Addition of two numbers is : "+num3);
}
static int Sum(int x,int y)
{
return x+y;
}
}
Output:
PS C:\Users\Pramod> & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Local\Temp\vscodesws_61e04\jdt_ws\jdt.ls-java-project\bin' 'Main'
Enter first number
1
Enter second number
2
Addition of two numbers is : 3
Test Code Here:
Recommended Post:
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Write a java program to Checks whether a given string is a palindrome or not
- Write a java program to perform multiplication of two matrices
- write a java program that prints the Fibonacci series for a given number.
- Write a Java program that finds the factorial of a number
- Write a Java program that finds prime numbers between 1 to n
- Write a Java program that prints all real and imaginary solutions to the quadratic equation
- Odd and Even number in java | Algorithm
- Even number in java | algorithm with source code
- Java greater number using a loop
- Java Area of Rectangle
- What is a polygon? | Area of Triangle in java
- To calculate the Area of Circle in the java program
- Java Addition through user input
- Addition program in java with source code
Find the solution to the salesforce Question.