Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program that demonstrate the basic arithmetic operators

Posted By: Volker Fischer     Category: Java     Views: 5647

Write a program that demonstrate the basic arithmetic operators.

Code for Program that demonstrate the basic arithmetic operators in Java

class BasicMath {
  publicstaticvoid main(String args[]) {
    // arithmetic using integers
    System.out.println("Integer Arithmetic");
    int a = 1 + 1;
    int b = a * 3;
    int c = b / 4;
    int d = c - a;
    int e = -d;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    System.out.println("d = " + d);
    System.out.println("e = " + e);

    // arithmetic using doubles
    System.out.println("\nFloating Point Arithmetic");
    double da = 1 + 1;
    double db = da * 3;
    double dc = db / 4;
    double dd = dc - a;
    double de = -dd;
    System.out.println("da = " + da);
    System.out.println("db = " + db);
    System.out.println("dc = " + dc);
    System.out.println("dd = " + dd);
    System.out.println("de = " + de);
  }
}

listing 2
// Demonstrate the % operator.class Modulus {
  publicstaticvoid main(String args[]) {
    int x = 42;
    double y = 42.25;

    System.out.println("x mod 10 = " + x % 10);
    System.out.println("y mod 10 = " + y % 10);
  }
}

listing 3
a = a + 4;

listing 4
a += 4;

listing 5
a = a % 2;

listing 6
a %= 2;

listing 7
// Demonstrate several assignment operators.class OpEquals {
  publicstaticvoid main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;

    a += 5;
    b *= 4;
    c += a * b;
    c %= 6;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
  }
}

listing 8
x = x + 1;

listing 9
x++;

listing 10
x = x - 1;

listing 11
x--;

listing 12
x = 42;
y = ++x;

listing 13
x = x + 1;
y = x;

listing 14
x = 42;
y = x++;

listing 15
y = x;
x = x + 1;

listing 16
// Demonstrate ++ and --.class IncDec {
  publicstaticvoid main(String args[]) {
    int a = 1;
    int b = 2;
    int c;
    int d;

    c = ++b;
    d = a++;
    c++;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    System.out.println("d = " + d);
  }
}

listing 17
// Demonstrate the bitwise logical operators.class BitLogic {
  publicstaticvoid main(String args[]) {
    String binary[] = {
      "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
      "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
    };
    int a = 3; // 0 + 2 + 1 or 0011 in binaryint b = 6; // 4 + 2 + 0 or 0110 in binaryint c = a | b;
    int d = a & b;
    int e = a ^ b;
    int f = (~a & b) | (a & ~b);
    int g = ~a & 0x0f;

    System.out.println("        a = " + binary[a]);
    System.out.println("        b = " + binary[b]);
    System.out.println("      a|b = " + binary[c]);
    System.out.println("      a&b = " + binary[d]);
    System.out.println("      a^b = " + binary[e]);
    System.out.println("~a&b|a&~b = " + binary[f]);
    System.out.println("       ~a = " + binary[g]);
  }
}

listing 18
// Left shifting a byte value.class ByteShift {
  publicstaticvoid main(String args[]) {
    byte a = 64, b;
    int i;

    i = a << 2;
    b = (byte) (a << 2);

    System.out.println("Original value of a: " + a);
    System.out.println("i and b: " + i + " " + b);
  }
}

listing 19
// Left shifting as a quick way to multiply by 2.class MultByTwo {
  publicstaticvoid main(String args[]) {
    int i;
    int num = 0xFFFFFFE;

    for(i=0; i<4; i++) {
      num = num << 1;
      System.out.println(num);
    }
  }
}

listing 20
int a = 32;
a = a >> 2; // a now contains 8

listing 21
int a = 35;
a = a >> 2; // a still contains 8

listing 22
// Masking sign extension.class HexByte {
  staticpublicvoid main(String args[]) {
    char hex[] = {
      '0', '1', '2', '3', '4', '5', '6', '7',
      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };
    byte b = (byte) 0xf1;

    System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
  }
}

listing 23
int a = -1;
a = a >>> 24;

listing 24
// Unsigned shifting a byte value.class ByteUShift {
  staticpublicvoid main(String args[]) {
    char hex[] = {
      '0', '1', '2', '3', '4', '5', '6', '7',
      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };
    byte b = (byte) 0xf1;
    byte c = (byte) (b >> 4);
    byte d = (byte) (b >>> 4);
    byte e = (byte) ((b & 0xff) >> 4);

    System.out.println("              b = 0x"
      + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
    System.out.println("         b >> 4 = 0x"
      + hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
    System.out.println("        b >>> 4 = 0x"
      + hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
    System.out.println("(b & 0xff) >> 4 = 0x"
      + hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
  }
}

listing 25
a = a >> 4;
a >>= 4;

listing 26
a = a | b;
a |= b;

listing 27
class OpBitEquals {
  publicstaticvoid main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;

    a |= 4;
    b >>= 1;
    c <<= 1;
    a ^= c;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
  }
}

listing 28
int a = 4;
int b = 1;
boolean c = a < b;

listing 29
int done;
// ...if(!done) ... // Valid in C/C++if(num) ...   // but not in Java.

listing 30
if(done == 0)) ... // This is Java-style.if(num != 0) ...

listing 31
// Demonstrate the boolean logical operators.class BoolLogic {
  publicstaticvoid main(String args[]) {
    boolean a = true;
    boolean b = false;
    boolean c = a | b;
    boolean d = a & b;
    boolean e = a ^ b;
    boolean f = (!a & b) | (a & !b);
    boolean g = !a;

    System.out.println("        a = " + a);
    System.out.println("        b = " + b);
    System.out.println("      a|b = " + c);
    System.out.println("      a&b = " + d);
    System.out.println("      a^b = " + e);
    System.out.println("!a&b|a&!b = " + f);
    System.out.println("       !a = " + g);
  }
}

listing 32
if (denom != 0 && num / denom > 10)

listing 33
if(c==1 & e++ < 100) d = 100;

listing 34
int x, y, z;

x = y = z = 100; // set x, y, and z to 100

listing 35
ratio = denom == 0 ? 0 : num / denom;

listing 36
// Demonstrate ?. class Ternary {
  publicstaticvoid main(String args[]) {
    int i, k;

    i = 10;
    k = i < 0 ? -i : i; // get absolute value of i
    System.out.print("Absolute value of ");
    System.out.println(i + " is " + k);

    i = -10;
    k = i < 0 ? -i : i; // get absolute value of i
    System.out.print("Absolute value of ");
    System.out.println(i + " is " + k);
  }
}

listing 37
a >> b + 3

listing 38
a >> (b + 3)

listing 39
(a >> b) + 3

listing 40
a | 4 + c >> b & 7 || b > a % 3
(a | (((4 + c) >> b) & 7)) || (b > (a % 3))


  
Share: 


Didn't find what you were looking for? Find more on Program that demonstrate the basic arithmetic operators Or get search suggestion and latest updates.

Volker Fischer
Volker Fischer author of Program that demonstrate the basic arithmetic operators is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!