Java program to perform operations without using third variable

 Java program to perform operations without using third variable

 Java program to perform addition without using third variable:

Program:

import java.util.*; 
class WithOutThirdVar 
{
public static void main(String args[]) 
{
Scanner sc= new Scanner(System.in); 
System.out.println("Enter value of A");
int a =sc.nextInt(); 
System.out.println("Enter value of B"); 
int b =sc.nextInt(); 
System.out.println("Addition is : "+(a+b));
}
}

Output:

Enter value of A
10
Enter value of B
5
Addition is : 15

 Java program to perform Substraction without using third variable:

Program:

import java.util.*; 
class WithOutThirdVar 
{
public static void main(String args[]) 
{
Scanner sc= new Scanner(System.in); 
System.out.println("Enter value of A");
int a =sc.nextInt(); 
System.out.println("Enter value of B"); 
int b =sc.nextInt(); 
System.out.println("Substraction is : "+(a-b));
}
}

Output:

Enter value of A
11
Enter value of B
4
Substraction is : 15

Java program to perform Multiplication without using third variable:

Program:

import java.util.*; 
class WithOutThirdVar 
{
public static void main(String args[]) 
{
Scanner sc= new Scanner(System.in); 
System.out.println("Enter value of A");
int a =sc.nextInt(); 
System.out.println("Enter value of B"); 
int b =sc.nextInt(); 
System.out.println("Multiplication is : "+(a*b));
}
}

Output:

Enter value of A
4
Enter value of B
6
Multiplication is : 24

Java program to perform Division without using third variable:

Program:

import java.util.*; 
class WithOutThirdVar 
{
public static void main(String args[]) 
{
Scanner sc= new Scanner(System.in); 
System.out.println("Enter value of A");
int a =sc.nextInt(); 
System.out.println("Enter value of B"); 
int b =sc.nextInt(); 
System.out.println("Division is : "+(a/b));
}
}

Output:

Enter value of A
10
Enter value of B
5
Division is : 2

Recommended Post:

1. Java program to perform addition of two numbers without using third variable?

2.Java program to perform substraction of two numbers without using third variable?

3. Java program to perform multiplication of two numbers without using third variable?

4. Java program to perform division of two numbers without using third variable?

Leave a Comment