• Home
  • Introduction
  • Compiling and Running an Application
  • Basic Language Elements
  • Object Oriented Programming
  • objects
  • Bitwise operators
  • Samples
  • Java Control Flow Statements

Sample 4
Aritmetic operators
public class NumericalExamples {

public static void main (String [] args) {
System.out.print("2 + 3.0 = ");
System.out.println(2 + 3.0);
System.out.print("2 + 3 = ");
System.out.println(2 + 3);
System.out.print("9.0 / 4 = ");
System.out.println(9.0 / 4);
System.out.print("9 / 4 = ");
System.out.println(9 / 4);
}
}

Output
2 + 3.0 = 5.0
2 + 3 = 5
9.0 / 4 = 2.25
9 / 4 = 2

Sample 5
Series Summation Program
public class SeriesSum {
public static void main (String [] args) {
int n = 9;
System.out.println((n * (n + 1)) / 2);
}
}

Output
45

Sample 6

public class RelationalOperatorsDemo {
  public RelationalOperatorsDemo( ) {

    int x = 10, y = 5;
    System.out.println("x > y : "+(x > y));
    System.out.println("x < y : "+(x < y));
    System.out.println("x >= y : "+(x >= y));
    System.out.println("x <= y : "+(x <= y));
    System.out.println("x == y : "+(x == y));
    System.out.println("x != y : "+(x != y));
    }

    public static void main(String args[]){
           new RelationalOperatorsDemo();
    }
   }

Sample 7

public class LogicalOperatorsDemo {

       public LogicalOperatorsDemo() {
              boolean x = true;
              boolean y = false;
              System.out.println("x & y : " + (x & y));
              System.out.println("x && y : " + (x && y));
              System.out.println("x | y : " + (x | y));
              System.out.println("x || y: " + (x || y));
              System.out.println("x ^ y : " + (x ^ y));
              System.out.println("!x : " + (!x));
       }
       public static void main(String args[]) {
             new LogicalOperatorsDemo();
       }
 }
Sample 8

public class BitwiseOperatorsDemo {
public BitwiseOperatorsDemo() {
      int x = 0xFAEF; //1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1
      int y = 0xF8E9; //1 1 1 1 1 0 0 0 1 1 1 0 1 0 0 1
      int z; System.out.println("x & y : " + (x & y));
      System.out.println("x | y : " + (x | y));
      System.out.println("x ^ y : " + (x ^ y));
      System.out.println("~x : " + (~x));
      System.out.println("x << y : " + (x << y));
      System.out.println("x >> y : " + (x >> y));
      System.out.println("x >>> y : " + (x >>> y));
      //There is no unsigned left shift operator
      }

public static void main(String args[])
    new BitwiseOperatorsDemo();

    }

Sample 9

public class BitwisePrecedenceEx {

     public static void main(String[] args) {
          int a = 1 | 2 ^ 3 & 5;
          int b = ((1 | 2) ^ 3) & 5;
          int c = 1 | (2 ^ (3 & 5));
          System.out.print(a + "," + b + "," + c);
     }
}

Sample 10
public class CompoundOperatorsDemo {

     public CompoundOperatorsDemo() {
          int x = 0, y = 5;
          x += 3;
          System.out.println("x : " + x);
          y *= x;
          System.out.println("y : " + y);
          /*Similarly other operators can be applied as shortcuts. Other

          compound assignment operators include boolean logical

          , bitwiseand shift operators*/

    }
    public static void main(String args[]) {
          new CompoundOperatorsDemo();
     }
}

Sample 11

public class TernaryOperatorsDemo {



     public TernaryOperatorsDemo() {
          int x = 10, y = 12, z = 0;
          z = x > y ? x : y;
          System.out.println("z : " + z);
     }
     public static void main(String args[]) {
           new TernaryOperatorsDemo();
     }
}

Sample12
/*
* The following programs shows that when no explicit parenthesis is used then the
conditional operator
* evaluation is from right to left
*/

public class BooleanEx1 {

    static String m1(boolean b) {
         return b ? "T" : "F";
    }
    public static void main(String[] args) {
         boolean t1 = false ? false : true ? false : true ? false : true;
         boolean t2 = false ? false
                                 : (true ? false : (true ? false : true));
         boolean t3 = ((false ? false : true) ? false : true) ? false
                                 : true;
         System.out.println(m1(t1) + m1(t2) + m1(t3));
    }
}

Sample 13

//program for checking Armstrong Number
//An Armstromg number is one which is equal to the sum of the cubes of its digits
//Eg. 153=1³+5³+3³=1+125+27=153...

import java.io.*;
class armstrong
{
public static void main(String aa[]) throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a, b=0, c, d;
System.out.println("Enter a number to check for Armstong");
String S=br.readLine();
a=Integer.parseInt(S);
c=a;
while(c!=0)
{
d=c%10;
c=c/10;
b+=d*d*d;
}
if(a==b)
System.out.println(a+" is an Armstrong no.");
else
System.out.println(a+" is NOT an Armstrong no.");
}
}




Sample 14

//To Print entered number in reverse
import java.io.*;
class abc
{
public static void main(Stringa aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence");
String S=br.readLine();
S=S.trim();
S=" "+S+" ";
int L=S.length(),a=L;
for(int i=L-1; i>=0; i--)
{
if(S.charAt(i)==32)
{
System.out.print(S.substring(i+1,b)+" ");
b=i;
}
}
}
}

Sample 15

public class TrueFalse {
    public static void main(String[] args) {
          boolean boolVar;
          boolVar = (2 < 3);
          System.out.println("2 < 3? Answer is " + boolVar);
          boolVar = (4 == 5); System.out.println("4 == 5? Answer is " + boolVar);
    }
}



Copyright 2019, Nationalinstituteonline.com

Powered by National3c.com