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: