Write a program to input three integers and find their sum, without using the mathematical operator plus in java
Answers
Answer:
import java.util.Scanner;
public class Example111 {
public static void main(String[] arg)
{
int x, y ;
Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
x = in.nextInt();
System.out.print("Input second number: ");
y = in.nextInt();
while(y != 0){
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
System.out.print("Sum: "+x);
System.out.print("\n");
}
}
Answer:
import java.util.Scanner;
public class Example111 {
public static void main(String[] arg)
{
int x, y ;
Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
x = in.nextInt();
System.out.print("Input second number: ");
y = in.nextInt();
while(y != 0){
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
System.out.print("Sum: "+x);
System.out.print("\n");
}
}
Explanation: