package demo;
import java.util.Scanner;
public class Addition
{
public static void main(String[] args)
{
int a, b;
int sum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any two integers: ");
a = sc.nextInt();
b = sc.nextInt();
sum = a - ~b - 1;
System.out.print("Sum of two integers: " + sum);
}
}
Output:
Enter any two integers: 20
50
Sum of two integers: 70
BUILD SUCCESSFUL (total time: 10 seconds)
import java.util.Scanner;
public class Addition
{
public static void main(String[] args)
{
int a, b;
int sum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any two integers: ");
a = sc.nextInt();
b = sc.nextInt();
sum = a - ~b - 1;
System.out.print("Sum of two integers: " + sum);
}
}
Output:
Enter any two integers: 20
50
Sum of two integers: 70
BUILD SUCCESSFUL (total time: 10 seconds)
Algorithm:
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b
|
No comments:
Post a Comment