Java Program to Find Area of Rhombus

In the previous article we have discussed Java Program to Find Perimeter of Parallelogram

In this article we will discuss about how to find area of rhombus.

Program to Find Area of Rhombus

Before jumping into the program directly, let’s first see how we calculate area of rhombus.

Formula for area of Parallelogram : (d1+d2)/2

Where,

  • length‘ represents length of parallelogram.
  • breadth‘ represents height of parallelogram.

Let’s see different ways to do it.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Method-1: Java Program to Find Area of Rhombus By Static Value

In this method, diagonal1 and diagonal2 values are already declared in the program. Then these value will be used to calculate the area of rhombus using the formula and then the result will be displayed.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main
{
   public static void main(String args[]) 
    {   
        //Scanner class object created
        Scanner s= new Scanner(System.in);
        //diagoonal1 length declared
        double d1= 15;
        //diagoonal2 length declared
        double d2= 20;
   
        //finding area of rhombus
        double area=(d1*d2)/2;
        System.out.println("Area of Rhombus : " + area); 
    }
}
Output:

Area of Rhombus : 150.0

Method-2: Java Program to Find Area of Rhombus By User Input Value

In this method, we have taken the diagonal1 and diagonal2 input of rhombus as user input means it will ask the user to input the value for diagonal1 and diagonal2 . Then these value will be used to calculate the area of rhombus using the formula and then the result will be displayed.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main
{
   public static void main(String args[]) 
    {   
        //Scanner class object created
        Scanner s= new Scanner(System.in);
        //taking input of diagonal1 length
        System.out.println("Enter the length of diagonal1 : ");
        double d1= s.nextDouble();
        //taking input of diagonal2 length
        System.out.println("Enter the length of diagonal2 : ");
        double d2= s.nextDouble();
        //finding area of rhombus
        double area=(d1*d2)/2;
        System.out.println("Area of Rhombus : " + area);      
   }
}
Output:

Enter the length of diagonal1 : 4.5
Enter the length of diagonal2 : 5.6
Area of Rhombus : 12.6

Method-3: Java Program to Find Area of Rhombus By User Defined Method

In this method, we have implemented the same area finding logic within a user defined method say findArea() and passed the diagonal1 and diagonal2 length of rhombus as parameter to the method. This findArea() method will find the area and will display the result.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main
{
   public static void main(String args[]) 
    {   
        //Scanner class object created
        Scanner s= new Scanner(System.in);
        //taking input of diagonal1 length
        System.out.println("Enter the length of diagonal1 : ");
        double d1= s.nextDouble();
        //taking input of diagonal2 length
        System.out.println("Enter the length of diagonal2 : ");
        double d2= s.nextDouble();
        findArea(d1,d2);
    }     
        public static void findArea(double d1, double d2)
        {
        //finding area of rhombus
        double area=(d1*d2)/2;
        System.out.println("Area of Rhombus : " + area); 
        }
}
Output:

Enter the length of diagonal1 : 4.5
Enter the length of diagonal2 : 5.6
Area of Rhombus : 12.6

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs:

Java Program to Find Area of Equilateral Triangle

In the previous article we have discussed Java Program to Find Area of Isosceles Triangle

In this article we will discuss about how to find area of equilateral triangle.

Program to Find Area of Equilateral Triangle

Before jumping into the program directly, let’s first see how we calculate area of equilateral triangle.

Formula for Area of Equilateral Triangle: sqrt(3)/4)*(side*side)

Where,

  • side‘ represents side length of the equilateral triangle.
  • sqrt is the square root. (here square root of 3)

Let’s see different ways to do it.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method-1: Java Program to Find Area of Equilateral Triangle By Using Static Value

In this approach the side length of equilateral triangle is already defined in the program, which will be used to calculate the area based on the area calculation formula.

So, let’s see the program to understand it more clearly.

import java.util.*;

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

          //Side length of equilateral triangle is declared
          double side= 5.6;
          //calculating the area using the formula
          double  area=(Math.sqrt(3)/4)*(side*side);
          System.out.println("Area of Triangle : " + area);      
     }
}
Output:

Area of Triangle : 13.579278331339996

Method-2: Java Program to Find Area of Equilateral Triangle By Using User Input Value

In this approach the side length of equilateral triangle will be taken as input from the user, which will be used to calculate the area based on the area calculation formula.

So, let’s see the program to understand it more clearly.

import java.util.*;

public class Main
{
   public static void main(String args[]) 
    {   
          //Scanner class Object Created
          Scanner sc= new Scanner(System.in);
          //Taking side length input of the triangle from the user
          System.out.println("Enter side length of the triangle :");
          double side= sc.nextDouble();
          //calculating the area using the formula
          double  area=(Math.sqrt(3)/4)*(side*side);
          System.out.println("Area of Triangle : " + area);      
     }
}
Output:

Enter side length of the triangle : 5.6
Area of Triangle : 13.579278331339996

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs:

Java Program to Check Special Number

In the previous article, we have discussed Java Program to Check Palindrome Number

In this article we are going to understand what Special number is and how we can check whether a number is Special or not in Java with examples.

Program to Check Special Number

Special numbers are numbers whose factorial of individual digits adds up to the number itself.

 Example :

145: 1!+4!+5!= 1+24+120 = 145 Special number
19: 1!+9!=1+362880=362881 Not a Special number
124: 1!+2!+4!= 1+2+24=27 Not a Special number

In the above examples the numbers 19 and 124 are not special numbers as their factorials don’t add up to the numbers. Hence 145 is the only Special number here.

Let’s see different ways to check special number.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Approach :

  1. Enter/Declare a number and store it .
  2. We calculate the factorials of each digit using a function and add them.
  3. If the resultant sum is the same as the entered number, then the number is said to be a Special number.

Method-1:Java Program to Check Special Number By Using Static Value

import java.util.Scanner;
public class SpecialNumber{
    public static void main(String args[])
    {
        //A number declared;
        int num = 145;

        int temp = num,remainder, sum =0;
        //Loop to iterate through digits and add their factorials
        while(temp>0)
        {
            remainder = temp%10;
            sum+= factorialOf(remainder);
            temp = temp /10;
        }

        if(sum==num)
        {
            System.out.println(num+" is a special number");
        }
        else
        {
            System.out.println(num+" is not a special number");
        }
    }

    // Function that returns the factorial of the number
    static int factorialOf(int num)
    {
        int prod = 1;
        while(num>0)
        {
            prod = prod*num;
            num--;
        }
        return prod;
    }
}

Output:

145 is a special number

Method-2: Java Program to Check Special Number By User Input Value

import java.util.Scanner;
public class SpecialNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        int temp = num,remainder, sum =0;
        //Loop to iterate through digits and add their factorials
        while(temp>0)
        {
            remainder = temp%10;
            sum+= factorialOf(remainder);
            temp = temp /10;
        }

        if(sum==num)
        {
            System.out.println(num+" is a special number");
        }
        else
        {
            System.out.println(num+" is not a special number");
        }
    }

    // Function that returns the factorial of the number
    static int factorialOf(int num)
    {
        int prod = 1;
        while(num>0)
        {
            prod = prod*num;
            num--;
        }
        return prod;
    }
}

Output:

Case-1
Enter a number : 145
145 is a special number

Case-2

Enter a number : 124 
124 is a special number

Method-3: Java Program to Check Special Number By User Defined Method

import java.util.Scanner;
public class SpecialNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        //calling the user defined method
        //to check Special number or not.
        checkNumber(num);
    }

    //checkNumber() method to check special number
    public static void checkNumber(int num)
    {

        int temp = num,remainder, sum =0;
        //Loop to iterate through digits and add their factorials
        while(temp>0)
        {
            remainder = temp%10;
            sum+= factorialOf(remainder);
            temp = temp /10;
        }

        if(sum==num)
        {
            System.out.println(num+" is a special number");
        }
        else
        {
            System.out.println(num+" is not a special number");
        }
    }

    // Function that returns the factorial of the number
    static int factorialOf(int num)
    {
        int prod = 1;
        while(num>0)
        {
            prod = prod*num;
            num--;
        }
        return prod;
    }
}

 

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding

Related Java Programs:

Java Program to Check Kaprekar Number

In the previous article, we have discussed Java program to Check Deficient Number

In this article we are going to understand what Kaprekar number is and how we can check whether a number is Kaprekar or not in Java with examples.

Program to Check Kaprekar Number

Kaprekar numbers are numbers whose square can be divided into two parts which when added result in the original number.

Example :

  • 45: 452=2025; 20+25=45 Kaprekar Number
  • 40: 402= 1600; 16+00=16 Not Kaprekar Number
  • 9: 92 = 81; 8+1=9 Kaprekar Number

In the above examples the numbers 9 and 45 are Kaprekar numbers as sum of the halves of their squares adds up to the number itself. However 40 is not a Kaprekar number.

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Approach :

  1. We ask the user to enter/declare a number and store it .
  2. We square the number and then divide the digits into two parts. Then the two parts are added together.
  3. If the sum is the same as the entered number, then the number is said to be a Kaprekar number.

Program:

import java.util.Scanner;

public class KaprekarNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        if(iskaprekar(num))
        {
            System.out.println(num+" is a Kaprekar number");
        }
        else
        {
            System.out.println(num+" is Not a Kaprekar number");
        }
    }

    //method to check Kaprekar Number
    static boolean iskaprekar(int num)
    {
        // 1 is a Kaprekar number
        if (num == 1)
            return true;

        int squareNum = num * num;
        int count = 0;
        // Counting number of digits
        while (squareNum != 0)
        {
            count++;
            squareNum /= 10;
        }
    
        squareNum = num*num;
        for (int iter=1; iter<count; iter++)
        {
             // This avoids the number like 10, 100, 1000 as none of them are Kaprekar number
            int part = (int) Math.pow(10, iter);
            if (part == num)
                continue;
    
             //Adds both the halves
            int sum = squareNum/part + squareNum % part;
            //Checks whether both numbers are equal
            if (sum == num)
                return true;
        }
    
        return false;
    }
}
Output:

Case-1

Enter a number : 9
9 is a Kaprekar number

Case-2

Enter a number : 8
8 is a Kaprekar number

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Programs:

Java Program to Check Narcissistic Decimal Number

In the previous article, we have discussed Java program to Check Kaprekar Number

In this article we are going to understand what Narcisstic Decimal number is and how we can check whether a number is Narcisstic Decimal number or not in Java with examples.

Program to Check Narcissistic Decimal Number

Narcisstic Decimal numbers are non-negative numbers, whose digits when raised to the power of m, m being the number of digits, add up to the number itself.

Example :

  • 5: 51=5 Narcisstic Decimal number
  • 10: 12+02 = 1 Not a Narcisstic Decimal number
  • 153= 13+53+33=153 Narcisstic Decimal number

In the above examples the numbers 5 and 153 are Narcisstic Decimal numbers as their digits when raised to the power of number of digits is equal to the number itself. However 10 is not the Narcisstic Decimal number here.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Approach :

  1. Enter/declare a number and store it .
  2. We calculate the number of digits in the number and store it in a variable digits.
  3. The number is raised to the power stored in variable digits. Then all of them are added.
  4. If the sum is equal to the entered number, then the number is said to be a Narcisstic Decimal number.

Program:

import java.util.Scanner;

public class NarcissticDecimalNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        int sum = 0, temp = num, remainder, digits = numberOfDig(num);
        //Iterates through the digits and adds their raised power to sum
        while(temp>0)
        {
            remainder = temp%10;
            sum = sum + (int)Math.pow(remainder,digits);
            temp = temp/10;
        }

        if(sum==num)
        {
            System.out.println(num+" is a Narcisstic Decimal Number");
        }
        else
        {
            System.out.println(num+" is Not a Narcisstic Decimal Number");
        }
    }

    //Function that returns the number of digits
    static int numberOfDig(int num)
    {
        int digits = 0;
        while (num > 0)
        {
            digits++;
            num = num / 10;
        }
        return digits;
    }
}


Output:

Case-1

Enter a number : 153
153 is a Narcisstic Decimal Number

Case-2

Enter a number : 553
553 is a Narcisstic Decimal Number

Related Java Programs:

Java Program to Convert Octal to Decimal

In the previous article, we have discussed Java Program for Binary to Hexadecimal

In this article we will discuss about how to convert Octal to Decimal.

Program To Convert Octal To Decimal

Before jumping into the program directly, let’s first know about octal and decimal.

Decimal Number :

  • The number system with base 10 is generally called decimal number system .
  • This number system us usually consists of 10 digits i.e. 0,1,2,3,4,5,6,7,8,9
  • This is the popular number system which is used in daily life .
  • Example – (156)10 where “10” represents the base and “156” represents the decimal number.

Octal  Number :

  • The number system with base 8 is generally called octal number system .
  • This number system us usually consists of 8 digits i.e. 0,1,2,3,4,5,6,7
  • Example – (156)8 where “8” represents the base and “156” represents the octal
  • But (186)8 will be a wrong representation because the digits are possible between 0 to 7

Now while converting to the octal to decimal

As the base is 8 so We need to multiply the digits with the base value with the power of 8 .

For example : lets take a octal number system  (55)8  we need to convert with the equivalent decimal value . so ,

(55)8  = (5 × 8¹) + (5 × 8⁰)

= 40+5

= 45

Which can be represented as (45)10

Let’s see different ways to convert Octal to Decimal.

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Method 1 : Java Program for Octal to Decimal Using Integer.parseInt() method

Approach :

  • Take a octal value form the input .
  • Convert it into its decimal value by using Integer.parseInt(input value , 8);
  • Store it into a variable .
  • Print the result .

Program :

Let’s see the program to understand it more clearly.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
       // creating scanner object 
         Scanner sc = new Scanner(System.in);
    // input an  octal value as a string  through scanner class 
        System.out.println("Enter a octal Value : ");
        String input1=sc.next();
    // converting to decimal
        int output =Integer.parseInt(input1, 8);
        System.out.println("Converted integer is : "+output);
    }
}
Output :

Enter an octal Value : 55
Converted integer is :45

Method 2 : Java Program for Octal to Decimal By using mathematical approach

Approach :

  • Take a octal value form the input .
  • Take a for loop to iterate each digits of octal value and multiply with the power of 8 according to there position .
  • Each time store it into a intermediate variable .
  • After successful termination of loop Print the result .

Program :

Let’s see the program to understand it more clearly.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
    // creating scanner object 
         Scanner sc = new Scanner(System.in);
    // input an  octal value as a string  through scanner class 
        System.out.println("Enter a octal Value : ");
        int input1=sc.nextInt();
        int x = input1;
        int output=0;
    // converting to Integer 
     for (int a = 0; x > 0; a++) 
     {
            // Taking the last digit
            int temp = x % 10;
            // power on 8 suitable to  its position.
            double p = Math.pow(8, a);
            // Multiplying the digits to the Input and then adding it to result
            output += (temp * p);
            x = x / 10;
        }
  	
   System.out.println("Converted integer is :"+output);
  }
}
Output :

Enter an octal Value : 55
Converted integer is :45

Want to excel in java coding? Practice with these Java Programs examples with output and write any
kind of easy or difficult programs in the java language.

Related Java Programs:

Java Program to Find Volume and Surface Area of Sphere

In the previous article we have discussed Java Program to Find Area and Circumference of Circle

In this article we will discuss about how to find volume and surface area of sphere.

Program to Find Volume and Surface Area of Sphere

Before jumping into the program directly, let’s first know how can we get volume and surface area of sphere.

Formula for Volume of Sphere = (4/3)*pie*(radius*radius*radius)

Formula for Surface Area of Sphere = 4*pie*(radius*radius)

Where,

  • 'pie' represents PI value i.e. 3.141
  • 'radius' represents radius of sphere.

 Example:

Example- To find Volume of Sphere

When radius of  sphere  = 1
Then volume of sphere => area = (4/3)*pie*(radius*radius*radius)
                                      => area =  4.1887902047863905
Example- To find Surface Area of Sphere

When radius of sphere = 1
Then surface area of sphere  => Surface Area = 4*pie*(radius*radius)
                                              => Surface Area = 12.566370614359172

Now, let’s see the program.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Method-1: Java Program to Find Volume and Surface Area of Sphere By Using Static Values

In this, the radius value of the sphere  is already declared by the program.

Let’s see the program to know how it is actually implemented.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //radius of sphere declared
        double radius=1;
        
        //finding surface area of sphere
        double surfaceArea =  4 * Math.PI * radius * radius;
        //finding volume of sphere
        double volume = (4.0 / 3) * Math.PI * radius * radius * radius;

        System.out.println("Surface area of Sphere = "+surfaceArea);
        System.out.println("Volume of Sphere = "+ volume);
    }
}
Output:

Surface area of Sphere = 12.566370614359172
Volume of Sphere = 4.1887902047863905

Method-2: Java Program to Find Volume and Surface Area of Sphere By Using User Input Values

In this, the radius value of the sphere will be taken as input from the user.

Let’s see the program to know how it is actually implemented.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner sc=new Scanner(System.in);
        double radius, surfaceArea, volume;
        
        System.out.print("Enter the radius of Sphere = ");
        radius = sc.nextDouble();
        
        surfaceArea =  4 * Math.PI * radius * radius;
        volume = (4.0 / 3) * Math.PI * radius * radius * radius;

        System.out.println("Surface area of Sphere = "+surfaceArea);
        System.out.println("Volume of Sphere = "+ volume);
    }
}
Output:

Enter the radius of Sphere = 1
Surface area of Sphere = 12.566370614359172
Volume of Sphere = 4.1887902047863905

Method-3: Java Program to Find Volume and Surface Area of Sphere By Using User Defined Method

In this, the radius value of the sphere will be taken as input from the user. And that radius value will be passed as the parameter to the user defined method.

Let’s see the program to know how it is actually implemented.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        double radius;
        //taking radius input from user
        System.out.print("Please Enter the radius of a Sphere : ");
        radius = sc.nextDouble();
                //calling the calulate() method
        calculate(radius);
    }
        //user defined method i.e calculate() method
        // to find surface area and volume of sphere
    public static void calculate(double radius)
    {
        //finding surface area of sphere
        double surfaceArea =  4 * Math.PI * radius * radius;
        //finding volume of sphere
        double volume = (4.0 / 3) * Math.PI * radius * radius * radius;

        System.out.println("Surface area of Sphere = "+surfaceArea);
        System.out.println("Volume of Sphere = "+ volume);
    }
}
Output:

Enter the radius of Sphere = 1
Surface area of Sphere = 12.566370614359172
Volume of Sphere = 4.1887902047863905

Are you a job seeker and trying to find simple java programs for Interview? This would be the right
choice for you, just tap on the link and start preparing the java programs covered to crack the
interview.

Related Java Programs:

Java Program to Find Area and Perimeter of Rectangle

In the previous article we have discussed Java Program to Find Perimeter of Triangle

In this article we will discuss about how to find area and perimeter of a rectangle.

Program to Find Area and Perimeter of Rectangle

Before going into the program, let’s see how we find the area and perimeter of a rectangle.

Formula for area of Rectangle = 2 * (length + breadth)

Formula for perimeter of Rectangle = length * breadth

Where,

  •  length represents the length of the rectangle. (longer side of rectangle)
  • breadth represents the breadth of the rectangle. (smaller side of rectangle)

Example:

Example- To find Area of Rectangle

When length of rectangle = 4
and breadth of rectangle = 5
Then area of rectangle => area = 2 * (length + breadth)
                                     => area = 2*(4+5)
                                     => area = 18
Example- To find Perimeter of Rectangle

When length of rectangle = 4
and breadth of rectangle = 5
Then area of rectangle => perimeter = (length * breadth)
                                     => perimeter = 4*5
                                     => perimeter =18

Now, let’s see the program.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Method-1: Java Program to Find Area and Perimeter of Rectangle By using static values

In this values for length and breadth of rectangle are already taken.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //variables declared
        double length, breadth, perimeter, area;
        //length value declared
        length = 4.2;
        // breadth value declared
        breadth = 2.0;
        //finding perimeter
        perimeter = 2 * (length + breadth);
        System.out.println("Perimeter of rectangle :"+perimeter);
        //finding area
        area = length * breadth;
        System.out.println("Area of rectangle :"+area);
    }
}
Output:

Perimeter of rectangle : 12.4
Area of rectangle : 8.4

Method-2: Java Program to Find Area and Perimeter of Rectangle By using dynamic values

In this values for length and breadth of rectangle will be taken by the user.

Let’s see the program to understand it more clearly.

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //variables declared
        int length, breadth, perimeter, area;
        Scanner sc = new Scanner(System.in);
        //taking length input from user
        System.out.print("Enter length of rectangle : ");
        length = sc.nextInt();
        //taking breadth input from user
        System.out.print("Enter breadth of rectangle : ");
        breadth = sc.nextInt();
        //finding perimeter
        perimeter = 2 * (length + breadth);
        System.out.println("Perimeter of rectangle : "+perimeter);
        //finding area
        area = length * breadth;
        System.out.println("Area of rectangle : "+area);
    }
}
Output:

Enter length of rectangle : 3
Enter breadth of rectangle : 4
Perimeter of rectangle : 14
Area of rectangle : 12

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs:

Java Program to Find Area of Parallelogram

In the previous article we have discussed Java Program to Find Area and Perimeter of Pentagon

In this article we will discuss about how to find the area of Parallelogram.

Program to Find Area of Parallelogram

Before jumping into the program directly, let’s first see how we calculate area of parallelogram.

Formula of Area of Parallelogram: base*height

Where,

  • base‘ represents the base of parallelogram.
  • height‘ represents the height of parallelogram.
Example- Area of Parallelogram

base = 3
height = 4
Area of Parallelogram = base*height
                                    =  3*4
                                    = 12

Let’s see different ways to do it.

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Method-1: Java Program to Find Area of Parallelogram By using Static Value

In this method, the base and height of the parallelogram is already defined in the program. And the area is calculated according to the formula by using this base and height values.

So, let’s see the program to see how it actually works.

import java.util.Scanner;

public class Main
{

 public static void main(String[] args) 
 {
    //creating Scanner class object
    Scanner sc=new Scanner(System.in);
    //base and height of parallelogram delared
    int base=4; 
    int height=5; 
    //calculating area of paralleologram
    int area=base*height;  
    System.out.println("Area of the parallelogram : "+area);  
 }
}
Output:

Area of the parallelogram : 20

Method-2: Java Program to Find Area of Parallelogram By Using User Input Value

In this method, the base and height of the parallelogram will be taken as user input. And the area is calculated according to the formula by using that base and height values which are inputted by the user.

So, let’s see the program to see how it actually works.

import java.util.Scanner;

public class Main
{

 public static void main(String[] args) 
 {
    //creating Scanner class object
    Scanner sc=new Scanner(System.in);
    //Enter base of parallelogram
    System.out.println("Enter base : ");
    int base=sc.nextInt(); 
    System.out.println("Enter height : ");
    int height=sc.nextInt();  
    int area=base*height;  
    System.out.println("Area of the parallelogram : "+area);  
 }
}
Output:

Enter base : 4
Enter height : 5
Area of the parallelogram : 20

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs:

Java Program to Find Area and Perimeter of Pentagon

Program to Find Area of Pentagon

In this article we will discuss about how to find the area of Pentagon.

Before jumping into the program directly, let’s first see how we calculate area of pentagon.

Formula of Area of Pentagon: (sqrt(5*(5+2*sqrt(5)))*pow(a,2))/4.0

Formula of Perimeter of Pentagon: 5a

Where,

  • a‘ represents the side length of Pentagon.
Example-To find area of pentagon

a=5.5
Area of Pentagon = (sqrt(5*(5+2*sqrt(5)))*pow(a,2))/4.0
                             = (sqrt(5*(5+2*sqrt(5)))*pow(5.5,2))/4.0
                             = 52.04444136781625
Example-To find perimeter of pentagon

a=5.5
Perimeter of Pentagon = 5a
                             = 5*5.5
                             = 27.5

Let’s see different ways to do it.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Method-1: Using Static Value

In this method, the side length of pentagon is already defined in the program. And the area and perimeter are calculated according to the formula by using this side length value.

So, let’s see the program to see how it actually works.

import java.util.Scanner;

public class Main
{

 public static void main(String[] args) 
 {
      //creating Scanner class object
      Scanner sc=new Scanner(System.in);
      //Enter side length of pentagon
      System.out.println("Enter side length of Pentagon :");
      double a = 5.5;
      double area = (Math.sqrt(5*(5+2*Math.sqrt(5)))*Math.pow(a,2))/4.0;
      double perimeter = (5*a);
      System.out.println("Area of Pentagon = "+area);
      System.out.println("Perimeter of Pentagon = "+perimeter);
 }
}
Output:

Enter side length of Pentagon : 5.5
Area of Pentagon = 52.04444136781625
Perimeter of Pentagon = 27.5

Method-2: Using User Defined Value

In this method, the side length of pentagon is taken as user input. And the area and perimeter are calculated according to the formula by using this side length value.

So, let’s see the program to see how it actually works.

import java.util.Scanner;

public class Main
{

 public static void main(String[] args) 
 {
      //creating Scanner class object
      Scanner sc=new Scanner(System.in);
      //Enter side length of pentagon
      System.out.println("Enter side length of Pentagon :");
      int a = sc.nextInt();
      double area = (Math.sqrt(5*(5+2*Math.sqrt(5)))*Math.pow(a,2))/4.0;
      int perimeter = (5*a);
      System.out.println("Area of Pentagon = "+area);
      System.out.println("Perimeter of Pentagon = "+perimeter);
 }
}
Output:

Enter side length of Pentagon : 5
Area of Pentagon = 43.01193501472417
Perimeter of Pentagon = 25