Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to calculate Area of Triangle through a Java program ?.

Program to calculate Area of Triangle in Java



import java.util.Scanner;

public class Triangle {

   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      double base = 0;
      double height = 0;
      double area = 0;

      System.out.print("Enter the length of base of triangle : "); 
      base = input.nextDouble();

      System.out.print("Enter the length of height of triangle : "); 
      height = input.nextDouble();

      area = (base * height) / 2;
    
      System.out.println("");
      System.out.println("The Area of Triangle is : "
                  + area);

   }

}


Output of the program : 


Program to demonstrate three dimensional Array in Java

Program to demonstrate three dimensional Array in Java

import java.util.Scanner;

public class ThreeDimensionalArrayDemo {
   public static void main(String args[]) {

       Scanner input = new Scanner(System.in);
  
       int threeDArray[][][] = new int[10][10][10];
       int i = 0, j = 0, k = 0;
  
       System.out.print("Enter the value of i : "); 
       i = input.nextInt();

       System.out.print("Enter the value of j : "); 
       j = input.nextInt();
        
       System.out.print("Enter the value of k : "); 
       k = input.nextInt();
  
       System.out.println("");
  
       for(int m=0; m < i; m++) 
  for(int n=0; n < j; n++)
    for(int p=0; p < k; p++)
      threeDArray[m][n][p] = m * n * p;

       for(int m=0; m < i; m++) {
  for(int n=0; n < j; n++) {
    for(int p=0; p < k; p++) 
      System.out.print(threeDArray[m][n][p] + " "); 
    System.out.println();
  }
  System.out.println();
  }
       }
}

Output of the program : 


 

How Cast Operator works in Java with example ?

Program to demonstrate how Cast Operator works in Java.

public class CastOperator {

  public static void main(String args[]) {
    
     byte b =0;
     int i = 358;
     double d = 462.142;

     System.out.println("Conversion of int to byte: ");
     b = (byte) i;
     System.out.println("i = "+ i +" and b = "+ b);

     System.out.println();
  
     System.out.println("Conversion of double to int: ");
     i = (int) d;
     System.out.println("d = "+ d +" and i = "+ i);
  
     System.out.println();
  
     System.out.println("Conversion of double to byte: ");
     b = (byte) d;
     System.out.println("d = "+ d +" and b = "+ b);
  }
}



Output of the program : 





 
© 2021 Learn Java by Examples Template by Hubberspot