Author: admin

  • Java Program to Calculate the Area of Different Geometric Figures

    The program shows how to Calculate the Area of Different Geometric Figures in java using a switch statement. The program uses a scanner class to take the inputs from the user.

    The switch case is mostly used when it is necessary to give options to the users. The following program gives users seven cases or options to choose from and it displays results accordingly.

    If you do not know the working process of the switch case statement then click the link below.


    Java Program to Calculate the Area of Different Geometric Figures using Switch Case

     //area of different geometric figures using switch case in java
    
     import java.util.Scanner;
    
     public class AreasSwitch
     {
      public static void main(String args[])
      {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("To find the areas of following press:");
        System.out.println("1 for the Area of a Circle");
        System.out.println("2 for the Area of a Square");
        System.out.println("3 for the Area of a Right Angled Triangle");
        System.out.println("4 for the Area of a Rectangle");
        System.out.println("5 for the Circumference of a Circle");
        System.out.println("6 for the Perimeter of a Square");
        System.out.println("7 for the Perimeter of a Rectangle");
        System.out.println("Enter your Choice:");
        int ch = sc.nextInt();
    
        switch(ch)
        {
          case 1: System.out.println("Enter radius for the circle:");
                  float r = sc.nextFloat();
                  float area = 3.14f*r*r;
                  System.out.println("Area:"+area);
                  break;
    
          case 2: System.out.println("Enter side for square:");
                  int s = sc.nextInt();
                  int ae = s*s;
                  System.out.println("Area:"+ae);
                  break;
    
          case 3: System.out.println("Enter height and base for traingle:");
                  float h = sc.nextFloat();
                  float bs = sc.nextFloat();
                  float ar = 0.5f*h*bs;
                  System.out.println("Area:"+ar);
                  break;
    
          case 4: System.out.println("Enter length and breadth for rectangle:");
                  int l = sc.nextInt();
                  int b = sc.nextInt();
                  int aa = l*b;
                  System.out.println("Area:"+aa);
                  break;
    
          case 5: System.out.println("Enter radius for circle:");
                  float rad = sc.nextFloat();
                  float cir = 3.14f*2f*rad;
                  System.out.println("Circumference:"+cir);
                  break;
    
    
          case 6: System.out.println("Enter side for square:");
                  int side = sc.nextInt();
                  int peri = 4*side;
                  System.out.println("Perimeter:"+peri);
                  break;
          
          case 7: System.out.println("Enter length and breadth for rectangle:");
                  int lg = sc.nextInt();
                  int br = sc.nextInt();
                  int per = 2*(lg+br);
                  System.out.println("Perimeter:"+per);
                  break;             
    
          default:System.out.println("Invalid Input");
                  break;
        }
      }
     }

    Output:

    To find the areas of following press:
    1 for the Area of a Circle
    2 for the Area of a Square
    3 for the Area of a Right Angled Triangle
    4 for the Area of a Rectangle
    5 for the Circumference of a Circle
    6 for the Perimeter of a Square
    7 for the Perimeter of a Rectangle
    Enter your Choice:
    4
    Enter length and breadth for rectangle:
    5
    8
    Area:40

  • Java Program to Check for the Negative Number

    The program shows how to Check for the Negative Number in java. This program is also a good demonstration to know the working process of the if-else statement.

    If you do not understand the program, perhaps you may want to learn about the if-else statement first.


    Java Program to Check for the Negative Number

     //check for the negative number using if-else statement in java
    
     public class IfElseNegNumber
     {
       public static void main(String[] args) 
       {
         int num = 5;
          
          if(num < 0)  //checking condition for true
          { 
            System.out.println("Number is negative");
          }
          else //executed if the condition is false
          { 
            System.out.println("Number is positive");
          }
            
          System.out.println("Always executed");
       }
       
     }

    Output:

    Number is positive
    Always executed


  • Java Program to List Even Numbers within a Range

    The program shows how to List Even Numbers within a Range in java. The program uses a scanner class to take the input of the last number(n) from the user.

    Therefore it displays all the even numbers between 1 to n using for loop and if statement.

    If you do not know the working process of for loop and if statement then click the link below.


    Java Program to List all the Even Numbers within a Range

     //list even numbers using for loop
    
     import java.util.Scanner;
    
     public class ListOfEvenNumbers
     {
      
       public static void main(String[] args) 
       {
         
         Scanner scanner = new Scanner(System.in);
         
         System.out.println("Enter the last number:");
         int num = scanner.nextInt();
     
         System.out.println("Displaying even numbers between 1 to " +num);
         
         for(int i=1; i <= num; i++)
         {
          // if the number is divisible by 2 then it is even
          if( i % 2 == 0)
          {
            System.out.print(i + " ");
          }
         }
       }
     }

    Output:

    Enter the last number:
    50
    Displaying even numbers between 1 to 50
    2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 

  • Java Program to Calculate Average of Elements in an Array

    The program shows how to Calculate the Average in an Array element in java. The program uses a scanner class to take the input from the user. It takes the size of an array and the array elements and adds those elements and displays them accordingly.

    If you do not know the working process of for loop then click the link below.


    Java Program to Calculate Average of Elements in an Array

     //calculate the average in an array element using for loop
    
     import java.util.Scanner;
    
     public class AverageForLoop
     {
        public static void main(String args[])
        {
          int num, sum = 0;
          float average;
          Scanner s = new Scanner(System.in);
          
          System.out.print("Enter the number of elements for an array: ");
          num = s.nextInt();
          
          int a[] = new int[num];
          
          System.out.println("Enter elements of array:");
          for(int i = 0; i < num; i++)
          {
              a[i] = s.nextInt();
          }
             
         
         for(int i=0; i < a.length ; i++)
           sum = sum + a[i];
         
         //calculate average value
         average = sum / num;
         
         System.out.println("Average value of array elements is : " +average);
            
        }
     }

    Output:

    Enter the number of elements for an array: 5
    Enter elements of array:
    1
    2
    3
    4
    5
    Average value of array elements is : 3.0


  • Java Program to Generate First n Whole Numbers using a do-while loop

    The program shows how to Generate First n Whole Numbers in java. The program is pretty simple n is already defined in a program. Although you can use the scanner class to take the inputs from the user.

    If you do not know the working process of do-while loop then click the link below.


    Java Program to Generate First n Whole Numbers

     //generate first n whole numbers using do-while loop 
    
     public class DoWhileNNumbers 
     {
        public static void main(String args[]) 
        {
          int num = 0;
          int N = 10;
        
          System.out.println("First 5 whole Number ");
          do
          {
           System.out.print(num + " ");
           num++; // Incrementing 
          } while ( num <= N );
        }
     }

    Output:

    First 5 whole Number
    0 1 2 3 4 5 6 7 8 9 10


  • Java Program to Count Backward of a Number using a do-while loop

    In this tutorial, we will write a program to count the number backward in java. The program uses a scanner class to take the input from the user and with a do-while loop, it counts backward and displays the result.

    If you do not know the working process of do-while loop then click the link below.


    Java Program to Count Backward of a Number

     //count backward of a number using do-while loop in java
    
     import java.util.Scanner;
    
     public class CountBackNumber
     {
       public static void main(String args[])
       {
         int n = 5;
         Scanner scanner = new Scanner(System.in);
    
         System.out.println("Enter the number from where you want to back count: ");
         n = scanner.nextInt();
    
         do {
           System.out.print(n + " ");
           n--;  // Decrementing N by 1 in each iteration
         } while (n >= 0);
       }
     }

    Output:

    Enter the number from where you want to back count:
    10
    10 9 8 7 6 5 4 3 2 1 0


  • Java Program to Find the Transpose of a Matrix

    In this tutorial, we will write a program to calculate the Transpose of a matrix in Java. As this program is based on Array and uses of for loop so you may go through the following first.

    Transpose of a matrix refers to converting the rows of a matrix into columns and columns of a matrix into rows. Consider a matrix A of 2 * 3 dimension, after transpose of A, the A matrix becomes 3 * 2 dimension.

    The Program takes the input for both the column and row elements from the user using the scanner class and transposes for that matrix is calculated. And lastly displays the final result with altered rows and columns.


    Java Program to Find the Transpose of a Matrix

      //transpose of a matrix in java
      
     import java.util.Scanner;
    
     public class TransposeOfMatrix
     {
        public static void main(String args[])
        {
            int row, col, i, j;
        
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the rows and columns of the matrix");
            row = in.nextInt();
            col = in.nextInt();
        
            int matrix[][] = new int[row][col];
            
            System.out.println("Enter the elements of matrix");
        
            for (i = 0; i < row; i++)
            {
             for (j = 0; j < col; j++)
             {
              matrix[i][j] = in.nextInt();
             }
            }
            int transpose[][] = new int[col][row];
            
            for (i = 0; i < row; i++)
            {
             for (j = 0; j < col; j++)  
             {            
                transpose[j][i] = matrix[i][j];
             }
            }
                
        
            System.out.println("Transpose of a entered matrix are:");
        
            for (i = 0; i < col; i++)
            {
             for (j = 0; j < row; j++)
             {
               System.out.print(transpose[i][j]+"\t");
             }
            System.out.print("\n");
            }
        }
     }

    Output:

     Enter the rows and columns of the matrix
     2
     3
     Enter the elements of matrix
     1
     2
     3
     4
     5
     6
     Transpose of a entered matrix are:
     1	 4	
     2	 5	
     3	 6

  • Java Program for the Multiplication of Two Matrices

    In this tutorial, we will write a java program to calculate the multiplication of two matrices. As this program is based on Array and uses of for loop so you may go through the following first.

    Matrices mean having two-dimension that is there are columns and rows in the following java example.

    The Program takes the input for both the column and row elements from the user using the scanner class and a third two-dimensional array is created to store the multiplication result of two matrices. And lastly displays the final result.


    Java Program to Multiply Two Matrices

     //multiplication of a matrix
      
     import java.util.Scanner;
    
     public class MultilicationMatrix
     {
        public static void main(String args[])
        {
        int r1, r2,c1,c2,i,j,k,sum;
        Scanner in = new Scanner(System.in);
        
        //first matrix
        System.out.println("Enter the number of rows of matrix1");
        r1 = in.nextInt();
        System.out.println("Enter the number columns of matrix 1");
        c1 = in.nextInt();
        
        //second matrix
        System.out.println("Enter the number of rows of matrix2");
        r2 = in.nextInt();
        System.out.println("Enter the number of columns of matrix 2");
        c2 = in.nextInt();
        
        //condition for multiplication
       if(c1==r2)
       {
        
        int mat1[][] = new int[r1][c1]; 
        int mat2[][] = new int[r2][c2]; 
        int result[][] = new int[r1][c2];
        
        System.out.println("Enter the elements of matrix1");
        for ( i= 0 ; i < r1 ; i++ )
        { 
         for ( j= 0 ; j < c1 ;j++ )
         {
          mat1[i][j] = in.nextInt();
         }
        }
        
        System.out.println("Enter the elements of matrix2");
        for ( i= 0 ; i < r2 ; i++ )
        { 
         for ( j= 0 ; j < c2 ;j++ )
         {
          mat2[i][j] = in.nextInt();
         }
        }
        
        System.out.println("Result of multiplication:-");
        for ( i= 0 ; i < r1 ; i++ )
        {
        
         for ( j = 0 ; j < c2; j++)
         {
            sum = 0;
            for ( k = 0 ; k < r2; k++ )
            {
             sum += mat1[i][k] * mat2[k][j] ;
            }
            result[i][j] = sum;
         }
        } 
        for ( i= 0 ; i < r1; i++ )
        {
         for ( j=0 ; j < c2; j++ )
         {
          System.out.print(result[i][j]+" ");
         }
          System.out.println();
         }
      }
      else
        System.out.print("Multiplication cannot be processed");
      }
        
     }

    Output:

     Enter the number of rows of matrix1
     2
     Enter the number columns of matrix 1
     3
     Enter the number of rows of matrix2
     3
     Enter the number of columns of matrix 2
     2
     Enter the elements of matrix1
     2
     3
     4 
     2
     1
     4
     Enter the elements of matrix2
     5
     4
     2 
     3
     5
     3
     Result of multiplication:
     36  29
     32  23

  • Java Program for the Subtraction of Two Matrices

    This tutorial shows how to subtract two matrices in Java. As this program is based on Array and uses of for loop so you may go through the following first.

    Matrices mean having two-dimension that is there are columns and rows in the following java example.

    The Program takes the input for both the column and row elements from the user using the scanner class and a third two-dimensional array is created to store the subtraction result of two matrices. And lastly displays the final result.


    Java Program to Subtract Two Matrices

      //subtraction of matrices in java
    
      import java.util.Scanner;
    
      public class SubtractionMatrix
      {
        public static void main(String args[])
        {
          int row, col, i, j;
          Scanner sc = new Scanner(System.in);
    
          System.out.println("Enter the number of rows");
          row = sc.nextInt();
    
          System.out.println("Enter the number  columns");
          col = sc.nextInt();
    
          int mat1[][] = new int[row][col];
          int mat2[][] = new int[row][col];
          int result[][] = new int[row][col];
    
          //First matrix Input
          System.out.println("Enter the elements of matrix1");
          for (i = 0; i < row; i++)
          {
    
            for (j = 0; j < col; j++)
              mat1[i][j] = sc.nextInt();
          }
    
          //Second matrix Input
          System.out.println("Enter the elements of  matrix2");
          for (i = 0; i < row; i++)
          {
    
            for (j = 0; j < col; j++)
              mat2[i][j] = sc.nextInt();
          }
    
          //Subtraction
          for (i = 0; i < row; i++)
            for (j = 0; j < col; j++)
              result[i][j] = mat1[i][j] - mat2[i][j];
    
          System.out.println("Result of subtraction:-");
    
          for (i = 0; i < row; i++)
          {
            for (j = 0; j < col; j++)
              System.out.print(result[i][j] + "\t");
    
            System.out.println();
          }
        }
      }

    Output:

     Enter the number of rows
     3
     Enter the number  columns
     2
     Enter the elements of matrix1
     4
     6
     8
     1
     2
     6
     Enter the elements of  matrix2
     0
     0
     0
     0
     0
     0
     Result of subtraction:-
     4	 6	
     8	 1	
     2	 6

  • Java Program for the Addition of Two Matrices

    In this tutorial, we will learn how to write a program to add two matrices in Java. As this program is based on Array and uses of for loop so you may go through the following first.

    Matrices mean having two-dimension that is there are columns and rows in the following java example.

    The Program takes the input for both the column and row elements from the user using the scanner class and a third two-dimensional array is created to store the sum result of two matrices. And lastly displays the final result.


    Java Program to Add Two Matrices

      //Addition of a matrix
    
     import java.util.Scanner;
    
     public class AdditionOfMatrix
     {
        public static void main(String args[])
        {
          int row, col, i, j;
          Scanner sc= new Scanner(System.in);
        
          System.out.println("Enter the number of rows");
          row = sc.nextInt();
            
          System.out.println("Enter the number  columns");
          col  = sc.nextInt();
        
          int mat1[][] = new int[row][col];
          int mat2[][] = new int[row][col];
          int result[][] = new int[row][col];
        
        //First matrix Input
          System.out.println("Enter the elements of matrix1");
          for (  i= 0 ; i < row ; i++ )
          {   
            for ( j= 0 ; j < col ;j++ )
            mat1[i][j] = sc.nextInt();
          }
            
         //Second matrix Input
          System.out.println("Enter the elements of  matrix2");
          for (  i= 0 ; i < row ; i++ )
           {
             for ( j= 0 ; j < col ;j++ )
             mat2[i][j] = sc.nextInt();
        
           }
        
        //Addition
          for (  i= 0 ; i < row ; i++ )
             for ( j= 0 ; j < col ;j++ )
                 result[i][j] =  mat1[i][j] + mat2[i][j]  ;   
        
          System.out.println("Result of Addition:-");
        
          for (  i= 0 ; i < row ; i++ )
          {  
            for ( j= 0 ; j < col ;j++ )
               System.out.print(result[i][j]+"\t");
            
            System.out.println();
         }
        
        }
     }

    Output:

     Enter the number of rows
     3
     Enter the number  columns
     2
     Enter the elements of matrix1
     1
     1
     1
     1
     1
     1
     Enter the elements of  matrix2
     1
     1
     1
     1
     1
     1
     Result of Addition:-
     2	 2	
     2 	 2	
     2 	 2