java program for finding gcd ,lcm ,xor and addition (without using operators )
import java.util.*;
class LCM
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int k1 = s.nextInt();
int k2= s.nextInt();
int gcd=hcf(k1,k2);
System.out.println(" hcf of these two number is "+gcd);
int lcm=(k1*k2)/gcd;
System.out.println(" lcm of these two number is "+lcm);
int xor=k1^k2;
System.out.println(" xor of these two number is " +xor);
int addd=Add(k1,k2);
System.out.println("addition method without using arthemetic operators is "+addd);
}
static int hcf(int a,int b)
{
int r=a%(b);
int diviser=a;
int divident=b;
int quotient;
while(r>0)
{
quotient=divident/diviser;
r=divident%diviser;
divident=diviser;
diviser=r;
}
return divident;
}
/*
Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.
*/
static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common set bits of x and y
int carry = x & y;
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding it to x gives the required sum
y = carry << 1;
}
return x;
}
}
class LCM
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int k1 = s.nextInt();
int k2= s.nextInt();
int gcd=hcf(k1,k2);
System.out.println(" hcf of these two number is "+gcd);
int lcm=(k1*k2)/gcd;
System.out.println(" lcm of these two number is "+lcm);
int xor=k1^k2;
System.out.println(" xor of these two number is " +xor);
int addd=Add(k1,k2);
System.out.println("addition method without using arthemetic operators is "+addd);
}
static int hcf(int a,int b)
{
int r=a%(b);
int diviser=a;
int divident=b;
int quotient;
while(r>0)
{
quotient=divident/diviser;
r=divident%diviser;
divident=diviser;
diviser=r;
}
return divident;
}
/*
Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.
*/
static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common set bits of x and y
int carry = x & y;
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding it to x gives the required sum
y = carry << 1;
}
return x;
}
}
Comments
Post a Comment