Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater

Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater

In the previous article, we have seen Java Program to Form Two Numbers (of 2 digit) with Minimum Sum Using Array Elements

In this article, we will how to find the index of array element, where all the elements before it are smaller and all the elements after it are greater by using Java programming language.

Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater

Prerequisite: 

See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.

Let’s see different ways to find index of array element, where all the elements before it are smaller and all the elements after it are greater.

Method-1: Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater By Static Initialization of Array Elements

Approach:

  • Declare an array with array elements as integers.
  • Then call user defined method find() to find index of array element, where all the elements before it are smaller and all the elements after it are greater.
  • Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are greater or not by using one for loop and check whether all elements left to it are smaller or not by using another for loop.
  • If condition satisfies then print that element with its index.

Program:

import java.util.*;
 
class Main
{
    //driver method
    public static void main(String[] args)
    {
        //declared an array
        int[] array = { 7,24,32,95,88,82 };
        
        //printing the original array
        System.out.println("Original array:");
        for(int i = 0; i < array.length ; i++)
            System.out.print(array[i]+" ");
        System.out.println();
        
        //finding index of array elements
        //where all elements before it are smaller 
        //and all elements after it are greater
        //by calling user defined method find()
        System.out.println("Array elements left to whose all are smaller and right to whose all are greater: ");
        int count=find(array);
        if(count==0)
        {
            System.out.print("No such element present in array");
        }
    }
    
    //find() method 
    public static int find(int[] array)
    {
        boolean left=false;
        boolean right=false;
        int count=0;
        int last=array.length;
        
        //iterating the array elements one by one
        for (int i=0; i<last-1; i++)
        {
            //checking all elements to current element's right are greater or not
            for(int j=i+1; j<array.length; j++)
            {
                //if right element is greater than current element i.e array[i] then assign 'true' to right variable
                //else assign 'false' to right variable and break the loop
                if(array[i]<array[j])
                {
                  right=true;  
                }
                else
                {
                  right=false;
                  break;
                }
            }
            
            //checking all elements to current element's left are smaller or not
            for(int k=i-1; k>=0; k--)
            {
                //if left element is smaller than current elementy i.e array[i] then assign 'true' to left variable
                //else assign 'false' to left variable and break the loop
                if(array[i]>array[k])
                {
                  left=true;  
                }
                else
                {
                  left=false;
                  break;
                }
            }
        
            //if left value is 'true' and right value is 'true' 
            //then left to array[i] all elements are smaller
            //and right to array[i] all elements are greater
            //so print that element with it's index
            if(left==true && right==true)
            {
                System.out.println("Element is "+array[i]+" and index is "+i);
                count++;
            }
        }
        return count;
    }
}
Output:

Original array:
7 24 32 95 88 82 
Array elements left to whose all are smaller and right to whose all are greater: 
Element is 24 and index is 1
Element is 32 and index is 2

Method-2: Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater By Dynamic Initialization of Array Elements

Approach:

  • Declare an array and take integer array elements as user input.
  • Then call user defined method find() to find index of array element, where all the elements before it are smaller and all the elements after it are greater.
  • Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are greater or not by using one for loop and check whether all elements left to it are smaller or not by using another for loop.
  • If condition satisfies then print that element with its index.

Program:

import java.util.*;
 
class Main
{
    //driver method
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int array[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        //taking input of array elemnts
        for (int i = 0; i < num; i++) 
        { 
        array[i] = sc.nextInt(); 
        }
        
        //printing the original array
        System.out.println("Original array:");
        for(int i = 0; i < array.length ; i++)
            System.out.print(array[i]+" ");
        System.out.println();
        
        //finding index of array elements
        //where all elements before it are smaller 
        //and all elements after it are greater
        //by calling user defined method find()
        System.out.println("Array elements left to whose all are smaller and right to whose all are greater: ");
        int count=find(array);
        if(count==0)
        {
            System.out.print("No such element present in array");
        }
    }
    
    //find() method 
    public static int find(int[] array)
    {
        boolean left=false;
        boolean right=false;
        int count=0;
        int last=array.length;
        //iterating the array elements one by one
        for (int i=0; i<last-1; i++)
        {
            //checking all elements to current element's right are greater or not
            for(int j=i+1; j<array.length; j++)
            {
                //if right element is greater than current element i.e array[i] then assign 'true' to right variable
                //else assign 'false' to right variable and break the loop
                if(array[i]<array[j])
                {
                  right=true;  
                }
                else
                {
                  right=false;
                  break;
                }
            }
            
            //checking all elements to current element's left are smaller or not
            for(int k=i-1; k>=0; k--)
            {
                //if left element is smaller than current element i.e array[i] then assign 'true' to left variable
                //else assign 'false' to left variable and break the loop
                if(array[i]>array[k])
                {
                  left=true;  
                }
                else
                {
                  left=false;
                  break;
                }
            }
        
            //if left value is 'true' and right value is 'true' 
            //then left to array[i] all elements are smaller
            //and right to array[i] all elements are greater
            //so print that element with it's index
            if(left==true && right==true)
            {
                System.out.println("Element is "+array[i]+" and index is "+i);
                count++;
            }
        }
        return count;
    }
}
Output:

Case-1
Enter the number of elements in the array: 6
Enter the elements: 10 20 30 50 40 60
Original array:
10 20 30 50 40 60 
Array elements left to whose all are smaller and right to whose all are greater: 
Element is 20 and index is 1
Element is 30 and index is 2

Case-2
Enter the number of elements in the array: 6
Enter the elements: 9 5 2 7 6 1
Original array:
9 5 2 7 6 1 
Array elements left to whose all are smaller and right to whose all are greater: 
No such element present in array

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.

Related Java Articles:

Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller

Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller

In the previous article, we have seen Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater

In this article, we will how to find the index of array element, where all the elements before it are greater and all the elements after it are smaller by using Java programming language.

Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller

Prerequisite: 

See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.

Let’s see different ways to find index of array element, where all the elements before it are greater and all the elements after it are smaller.

Method-1: Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller By Static Initialization of Array Elements

Approach:

  • Declare an array with array elements as integers.
  • Then call user defined method find() to find index of array element, where all the elements before it are greater and all the elements after it are smaller.
  • Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are smaller or not by using one for loop and check whether all elements left to it are greater or not by using another for loop.
  • If condition satisfies then print that element with its index.

Program:

import java.util.*;
 
class Main
{
    //driver method
    public static void main(String[] args)
    {
        //declared an array
        int[] array = { 57,44,32,15,28,18 };
        
        //printing the original array
        System.out.println("Original array:");
        for(int i = 0; i < array.length ; i++)
            System.out.print(array[i]+" ");
        System.out.println();
        
        //finding index of array elements
        //where all elements before it are greater 
        //and all elements after it are smaller
        //by calling user defined method find()
        System.out.println("Array elements left to whose all are greater and right to whose all are smaller: ");
        int count=find(array);
        if(count==0)
        {
            System.out.print("No such element present in array");
        }
    }
    
    //find() method 
    public static int find(int[] array)
    {
        boolean left=false;
        boolean right=false;
        int count=0;
        int last=array.length;
        
        //iterating the array elements one by one
        for (int i=0; i<last-1; i++)
        {
            //checking all elements to current element's right are smaller or not
            for(int j=i+1; j<array.length; j++)
            {
                //if right element is smaller than current element i.e array[i] then assign 'true' to right variable
                //else assign 'false' to right variable and break the loop
                if(array[i]>array[j])
                {
                  right=true;  
                }
                else
                {
                  right=false;
                  break;
                }
            }
            
            //checking all elements to current element's left are greater or not
            for(int k=i-1; k>=0; k--)
            {
                //if left element is greater than current element i.e array[i] then assign 'true' to left variable
                //else assign 'false' to left variable and break the loop
                if(array[i]<array[k])
                {
                  left=true;  
                }
                else
                {
                  left=false;
                  break;
                }
            }
        
            //if left value is 'true' and right value is 'true' 
            //then left to array[i] all elements are greater
            //and right to array[i] all elements are smaller
            //so print that element with it's index
            if(left==true && right==true)
            {
                System.out.println("Element is "+array[i]+" and index is "+i);
                count++;
            }
        }
        return count;
    }
}
Output:

Original array:
57 44 32 15 28 18 
Array elements left to whose all are greater and right to whose all are smaller: 
Element is 44 and index is 1
Element is 32 and index is 2

Method-2: Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller By Dynamic Initialization of Array Elements

Approach:

  • Declare an array and take integer array elements as user input.
  • Then call user defined method find() to find index of array element, where all the elements before it are greater and all the elements after it are smaller.
  • Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are smaller or not by using one for loop and check whether all elements left to it are greater or not by using another for loop.
  • If condition satisfies then print that element with its index.

Program:

import java.util.*;
 
class Main
{
    //driver method
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int array[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        //taking input of array elemnts
        for (int i = 0; i < num; i++) 
        { 
        array[i] = sc.nextInt(); 
        }
        
        //printing the original array
        System.out.println("Original array:");
        for(int i = 0; i < array.length ; i++)
            System.out.print(array[i]+" ");
        System.out.println();
        
        //finding index of array elements
        //where all elements before it are greater 
        //and all elements after it are smaller
        //by calling user defined method find()
        System.out.println("Array elements left to whose all are greater and right to whose all are smaller: ");
        int count=find(array);
        if(count==0)
        {
            System.out.print("No such element present in array");
        }
    }
    
    //find() method 
    public static int find(int[] array)
    {
        boolean left=false;
        boolean right=false;
        int count=0;
        int last=array.length;
        
        //iterating the array elements one by one
        for (int i=0; i<last-1; i++)
        {
            //checking all elements to current element's right are smaller or not
            for(int j=i+1; j<array.length; j++)
            {
                //if right element is smaller than current element i.e array[i] then assign 'true' to right variable
                //else assign 'false' to right variable and break the loop
                if(array[i]>array[j])
                {
                  right=true;  
                }
                else
                {
                  right=false;
                  break;
                }
            }
            
            //checking all elements to current element's left are greater or not
            for(int k=i-1; k>=0; k--)
            {
                //if left element is greater than current element i.e array[i] then assign 'true' to left variable
                //else assign 'false' to left variable and break the loop
                if(array[i]<array[k])
                {
                  left=true;  
                }
                else
                {
                  left=false;
                  break;
                }
            }
        
            //if left value is 'true' and right value is 'true' 
            //then left to array[i] all elements are greater
            //and right to array[i] all elements are smaller
            //so print that element with it's index
            if(left==true && right==true)
            {
                System.out.println("Element is "+array[i]+" and index is "+i);
                count++;
            }
        }
        return count;
    }
}
Output:

Case-2
Enter the number of elements in the array: 8
Enter the elements: 80 60 50 20 30 10 40 25
Original array:
80 60 50 20 30 10 40 25 
Array elements left to whose all are greater and right to whose all are smaller: 
Element is 60 and index is 1
Element is 50 and index is 2

Case-1
Enter the number of elements in the array: 8
Enter the elements: 8 4 6 2 1 5 3 7
Original array:
8 4 6 2 1 5 3 7 
Array elements left to whose all are greater and right to whose all are smaller: 
No such element present in array

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 Articles:

Java Program to Form Two Numbers (2 digit) with Minimum Sum Using Array Elements

Java Program to Form Two Numbers (2 digit) with Minimum Sum Using Array Elements

In the previous article, we have seen Java Program to Form Two Numbers (of 2 digit) with Maximum Sum Using Array Elements

In this article, we will see how to form two numbers of 2 digits with minimum sum using array elements in Java programming language.

Java Program to Form Two Numbers (2 digit) with Minimum Sum Using Array Elements

Prerequisite: 

See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.

Base Condition:

  1. The number is a 2 digited number (Ex-54, 89, 71 etc.)
  2. We have to form 2 numbers using array elements.
  3. All the array elements need to be single digited only.

Let’s see different ways to form two numbers of 2 digits with minimum sum.

Method-1: Java Program to Form Two Numbers (of 2 digits) with Minimum Sum Using Array Elements By Using Sorting

Approach:

  • Declare an array and with single digit numbers as array elements.
  • Call the user defined method findMinimum().
  • Inside method, sort the array in ascending order.
  • So first element of array is smallest element in array and elements are ordered in increasing order.
  • So combine index-0 and index-1 element which is the 1st smallest number that can be formed using array elements.
  • Next combine index-0 and index-2 element which is the 2nd smallest number that can be formed using array elements.
  • As we have two smallest numbers so sum will be minimum.
  • So, return those two numbers.

Program:

import java.util.Arrays;
import java.util.Comparator;
 
class Main
{
    // Find two numbers with a minimum sum formed by digits of an array
    public static void findMainimum(Integer[] input)
    {
 
        // sort the array in ascending order
        Arrays.sort(input);
 
        //find first number 
        int x = 0;
        x = x * 10 + input[0];
        x = x * 10 + input[1];
        
 
        //find second number
        int y=0;
        y = y * 10 + input[0];
        y = y * 10 + input[2];
       
        // print `x` and `y`
        System.out.println("The two numbers formed by using array elements with minimum sum are " + x + " and " + y);
        int sum=x+y;
        System.out.println("And the minimum sum is "+sum);        
    }
 
    public static void main(String[] args)
    {
        Integer[] input = { 1,5,3,4,5 };
 
        findMinimum(input);
    }
}
Output:

The two numbers formed by using array elements with minimum sum are 13 and 14
And the minimum sum is 27

Method-2: Java Program to Form Two Numbers (of 2 digits) with Minimum Sum Using Array Elements By Finding 1st, 2nd and 3rd Big Element from Array

Approach:

  • Take user input of length of array.
  • Take input of array elements (only single digits)
  • Call the user defined method findSmallest() to find 3 big elements in the array say firstNumber, secondNumber and thirdNumber.
  • After getting 3 small elements in the array, inside that findSmallest() method, call findMinNumber() method.
  • Inside findMinNumber() method combine firstNumber and secondNumber element which is the 1st smallest number that can be formed using array elements.
  • Next combine firstNumber and thirdNumber element which is the 2nd smallest number that can be formed using array elements.
  • As we have two smallest numbers so sum will be minimum.
  • So, return those two numbers.

Program:

import java.util.Scanner;

public class Main 
{
     public static void main(String args[]) 
    {
        int length = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of elements in array: ");
        //array length taken as input
        length = sc.nextInt();
        //array declared
        int[] arr = new int[length];
        
        //take input of single digited number as array element
        System.out.println("Enter elements into array: ");
        //taking input of array elements
        for (int i = 0; i < length; i++) 
        {
            arr[i] = sc.nextInt();
        }

        System.out.println("First 3 smallest numbers in array : ");
        //calling findSmallest() method
        findSmallest(arr);
    }
    
    //user defined method to find first 3 smallest element
    private static void findSmallest(int[] arr) 
    {
        int firstNumber = arr[0];
        int secondNumber = 0;
        int thirdNumber = 0;

        for (int i = 0; i < arr.length; i++) 
        { 
            if (arr[i] < firstNumber) 
            {
                thirdNumber = secondNumber;
                secondNumber = firstNumber;
                firstNumber = arr[i];
            } 
            else if (arr[i] < secondNumber) 
            {
                thirdNumber = secondNumber;
                secondNumber = arr[i];
            }
            else if (arr[i] < thirdNumber) 
            {
                thirdNumber = arr[i];
            }
        }

        System.out.println("First smallest number : " + firstNumber);
        System.out.println("Second smallest number : " + secondNumber);
        System.out.println("Third smallest number : " + thirdNumber);
        
        //calling findMinNumber() method
        findMinNumber(firstNumber,secondNumber,thirdNumber);
    }
    
    //findMinNumber() user defined method to form two numbers whose sum is minimum
    public static void findMinNumber(int firstNumber,int secondNumber,int thirdNumber)
    {
        //find first number 
        int x = 0; 
        x = x * 10 + firstNumber; 
        x = x * 10 + secondNumber;
        
        //find second number 
        int y = 0; 
        y = y * 10 + firstNumber; 
        y = y * 10 + thirdNumber;
        
        // print `x` and `y` 
        System.out.println("The two numbers formed by using array elements with minimum sum are " + x + " and " + y); 
        int sum=x+y; 
        System.out.println("And the minimum sum is "+sum);
    }
}
Output:

Enter number of elements in array: 
6
Enter elements into array: 
9 2 5 7 1 4
First 3 smallest numbers in array : 
First smallest number : 1
Second smallest number : 2
Third smallest number : 4
The two numbers formed by using array elements with minimum sum are 12 and 14
And the minimum sum is 26

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 Articles:

Java Program to Find Number of Elements Smaller Than a Given Number in a Given Sub Array

Java Program to Find Number of Elements Smaller Than a Given Number in a Given Sub Array

In the previous article, we have seen Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array

In this article we will see how to find number of elements smaller than a given number in a given sub array using Java programming language.

Java Program to Find Number of Elements Smaller Than a Given Number in a Given Sub Array

Prerequisite: 

See below articles to know more about Array, array declaration, array instantiation and array initialization.

Let’s see different ways how to find number of elements smaller than a given number in a given sub array.

Method-1: Java Program to Find Number of Elements Smaller Than a Given Number in a Given Sub Array By Static Initialization of Array Elements

Approach:

  • Declare an array say arr[] with numbers as array elements.
  • Enter a specific number and store it in an integer variable say int number which will be used for comparison.
  • Declare two integer variables say start and end and assign the start index and end index value to it by taking user input.
  • Then compare the specific number with all the elements of the sub array.
  • If any sub array element is smaller than that specific number then print it, as well as keep track on number of sub array elements which are smaller than that specific element by using an integer variable say count.
  • At last print the value of count as well.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        //array declared
        int arr[]={4,6,1,8,2,9,7,3,5};
        
        // Displaying the original array
        System.out.print("Original array: ");
        //printing the array
        for(int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        //Asking the user to take input of a number
        //We will find sub array elements which are smaller than this number
        System.out.print("Enter a specific number: "); 
        int number=sc.nextInt();
         
        //Entering the start and end index value for sub array
        System.out.print("Enter sub array start index: "); 
        int start=sc.nextInt();
        System.out.print("Enter sub array end index: "); 
        int end=sc.nextInt();
        
         // Displaying the sub array
        System.out.print("Sub array: ");
        //printing the array
        for(int i = start; i <= end; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        
        System.out.print("The numbers which are smaller than "+number+" are:");
        //count value initialized as 0
        //to keep track on number of elements which are smaller than the specific number.
        int count=0;
        for(int i = start; i <= end; i++)
        {
            if(arr[i]<number)
            {
                System.out.print(arr[i]+" ");
                count++;
            }
        }
        System.out.println();
        
        System.out.print("So, total "+count+" elements are smaller than "+number+" in the given sub array");
    }
}
Output:

Original array: 4 6 1 8 2 9 7 3 5 
Enter a specific number: 2
Enter sub array start index: 2
Enter sub array end index: 7
Sub array: 1 8 2 9 7 3 
The numbers which are smaller than 2 are:1 
So, total 1 elements are smaller than 2 in the given sub array

Method-2: Java Program to Find Number of Elements Smaller Than a Given Number in a Given Sub Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask user for input of array length.
  • Initialize the array with given size.
  • Ask the user for input of array elements.
  • Display the original array arr[].
  • Enter a specific number and store it in an integer variable say int number which will be used for comparison.
  • Declare two integer variables say start and end and assign the start index and end index value to it by taking user input.
  • Then compare the specific number with all the elements of the sub array.
  • If any sub array element is smaller than that specific number then print it, as well as keep track on number of sub array elements which are smaller than that specific element by using an integer variable say count.
  • At last print the value of count as well.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int arr[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        for (int i = 0; i < num; i++) 
        { 
        arr[i] = sc.nextInt(); 
        }
        
        // Displaying the original array
        System.out.print("Original array: ");
        //printing the array
        for(int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        //Asking the user to take input of a number
        //We will find sub array elements which are smaller than this number
        System.out.print("Enter a specific number: "); 
        int number=sc.nextInt();
         
        //Entering the start and end index value for sub array
        System.out.print("Enter sub array start index: "); 
        int start=sc.nextInt();
        System.out.print("Enter sub array end index: "); 
        int end=sc.nextInt();
        
         // Displaying the sub array
        System.out.print("Sub array: ");
        //printing the array
        for(int i = start; i <= end; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        
        System.out.print("The numbers which are smaller than "+number+" are:");
        //count value initialized as 0
        //to keep track on number of elements which are smaller than the specific number.
        int count=0;
        for(int i = start; i <= end; i++)
        {
            if(arr[i]<number)
            {
                System.out.print(arr[i]+" ");
                count++;
            }
        }
        System.out.println();
        
        System.out.print("So, total "+count+" elements are smaller than "+number+" in the given sub array");
    }
}
Output:

Enter the number of elements in the array: 9
Enter the elements: 1 2 3 4 5 6 7 8 9
Original array: 1 2 3 4 5 6 7 8 9 
Enter a specific number: 4
Enter sub array start index: 0 
Enter sub array end index: 6
Sub array: 1 2 3 4 5 6 7 
The numbers which are smaller than 4 are:1 2 3 
So, total 3 elements are smaller than 4 in the given sub array

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.

Related Java Articles:

Java Program to Check If Given Four Points Form a Square

Java Program to Check If Given Four Points Form a Square

In the previous article, we have seen Java Program to Find Total Area of Two Overlapping Rectangles

In this article, we will see how to check whether the given 4 points form a square or not by using Java programming language.

Java Program to Check If Given Four Points Form a Square

Before jumping into the program directly, let’s see how we can check that given four points form a square or not.

Explanation:

Let us assume there is a Quadrilateral having 4 points A,B,C,D

Now in order to check if the quadrilateral is a valid square or not, we have to check 2 conditions

  1. Distance of all the sides be equal I.e AB=BC=CD=DA
  2. Distance of diagonals be equal I.e AC=BD

Now in order to know the distance between 2 points we have the following formula as

AB = √[(x2−x1)2 + (y2−y1)2]

Where,

  • Coordinate of A is (x1,y1)
  • Coordinate of B is (x2,y2)

Example:

Let A(0,0), B(0,1), C(1,1), D(1,0)

Now,

AB =  √[(x2−x1)2 + (y2−y1)2] = √(0+1) =√1

BC = √(1+0) = √1

CD = √(0+1) = √1

DA = √(1+0) = √1

Since AB=BC=CD=DA, condition 1 satisfies.

Now,

AC = √(1+1) = √2

BD = √(1+1) = √2

Since AC=BD, condition 2 satisfies.

Since, both conditions satisfies hence we can say quadrilateral ABCD is a valid square.

Let’s see different ways to check whether the given 4 points form a square or not.

Method-1: Java Program to Check If Given Four Points Form a Square By Using Static Input Value

Approach:

  • Declare an integer variable say ‘x1’ & ‘y1’ and assign the value to it, which holds the coordinate value of the point A.
  • Declare an integer variable say ‘x2’ & ‘y2’ and assign the value to it, which holds the coordinate value of the point B.
  • Declare an integer variable say ‘x3’ & ‘y3’ and assign the value to it, which holds the coordinate value of the point C.
  • Declare an integer variable say ‘x4’ & ‘y4’ and assign the value to it, which holds the coordinate value of the point D.
  • Now, we will find the distance between all the points using the formula

AB = √(((x2−x1)*(x2−x1)) + ((y2−y1)*(y2−y1)))

BC = √(((x3−x2)*(x3−x2)) + ((y3−y2)*(y3−y2)))

CD = √(((x4−x3)*(x4−x3)) + ((y4−y3)*(y4−y3)))

DA = √(((x4−x1)*(x4−x1)) + ((y4−y1)*(y4−y1)))

AC = √(((x3−x1)*(x3−x1)) + ((y3−y1)*(y3−y1)))

BD = √(((x4−x2)*(x4−x2)) + ((y4−y2)*(y4−y2)))

  • Now, if AB==BC && BC==CD && CD==DA && AC==BD then print the result as valid square.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
            int x1 = 0;// x-cordinate of A
    		int y1 = 0;// y-cordinate of A
    		int x2 = 0;// x-cordinate of B
    		int y2 = 1;// y-cordinate of B
    		int x3 = 1;// x-cordinate of C
    		int y3 = 1;// y-cordinate of C
    		int x4 = 1;// x-cordinate of D
    		int y4 = 0;// y-cordinate of D
    		
            // distance formula using 2 point 2d fomula 
    		double AB = Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)));
    		double BC = Math.sqrt(((x3-x2)*(x3-x2)) + ((y3-y2)*(y3-y2)));
    		double CD = Math.sqrt(((x4-x3)*(x4-x3)) + ((y4-y3)*(y4-y3)));
    		double DA = Math.sqrt(((x4-x1)*(x4-x1)) + ((y4-y1)*(y4-y1)));
    		double AC = Math.sqrt(((x3-x1)*(x3-x1)) + ((y3-y1)*(y3-y1)));
    		double BD = Math.sqrt(((x4-x2)*(x4-x2)) + ((y4-y2)*(y4-y2)));
    		
            // checking conditions
    		if(AB==BC && BC==CD && CD==DA && AC==BD)
    		    System.out.println("Valid square");
    		else
    		    System.out.println("Not a valid square");
    }
}

Output:

Valid square

Method-2: Java Program to Check If Given Four Points Form a Square By Using Dynamic Input Value

Approach:

  • Declare an integer variable say ‘x1’ & ‘y1’ which holds the coordinate value of the point A.
  • Declare an integer variable say ‘x2’ & ‘y2’ which holds the coordinate value of the point B.
  • Declare an integer variable say ‘x3’ & ‘y3’ which holds the coordinate value of the point C.
  • Declare an integer variable say ‘x4’ & ‘y4’ which holds the coordinate value of the point D.
  • Then we will take the value of ‘x1’, ‘y1’, ‘x2’, ‘y2’, ‘r1’, ‘r2’ as user input using scanner class.
  • Now, we will find the distance between all the points using the formula

AB = √(((x2−x1)*(x2−x1)) + ((y2−y1)*(y2−y1)))

BC = √(((x3−x2)*(x3−x2)) + ((y3−y2)*(y3−y2)))

CD = √(((x4−x3)*(x4−x3)) + ((y4−y3)*(y4−y3)))

DA = √(((x4−x1)*(x4−x1)) + ((y4−y1)*(y4−y1)))

AC = √(((x3−x1)*(x3−x1)) + ((y3−y1)*(y3−y1)))

BD = √(((x4−x2)*(x4−x2)) + ((y4−y2)*(y4−y2)))

  • Now, if AB==BC && BC==CD && CD==DA && AC==BD then print the result as valid square.

Program:

import java.io.*;
import java.util.Scanner;
class Main
{
    public static void main(String [] args)
    {
        Scanner s = new Scanner(System.in); 
        System.out.println("Enter the value of x1 coordinate of A:");
        int x1 = s.nextInt(); // x-cordinate of A
        System.out.println("Enter the value of y1 coordinate of A:");
        int y1 = s.nextInt();// y-cordinate of A
        System.out.println("Enter the value of x1 coordinate of B:");
        int x2 = s.nextInt();// x-cordinate of B
        System.out.println("Enter the value of y1 coordinate of B:");
        int y2 = s.nextInt();// y-cordinate of B
        System.out.println("Enter the value of x1 coordinate of C:");
        int x3 = s.nextInt();// x-cordinate of C
        System.out.println("Enter the value of y1 coordinate of C:");
        int y3 = s.nextInt();// y-cordinate of C
        System.out.println("Enter the value of x1 coordinate of D:");
        int x4 = s.nextInt();// x-cordinate of D
        System.out.println("Enter the value of y1 coordinate of A:");
        int y4 = s.nextInt();// y-cordinate of D
        
        // distance formula using 2 point 2d fomula 
        double AB = Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)));
        double BC = Math.sqrt(((x3-x2)*(x3-x2)) + ((y3-y2)*(y3-y2)));
        double CD = Math.sqrt(((x4-x3)*(x4-x3)) + ((y4-y3)*(y4-y3)));
        double DA = Math.sqrt(((x4-x1)*(x4-x1)) + ((y4-y1)*(y4-y1)));
        double AC = Math.sqrt(((x3-x1)*(x3-x1)) + ((y3-y1)*(y3-y1)));
        double BD = Math.sqrt(((x4-x2)*(x4-x2)) + ((y4-y2)*(y4-y2)));
        
        // checking conditions
        if(AB==BC && BC==CD && CD==DA && AC==BD)
        System.out.println("Valid square");
        else
        System.out.println("Not a valid square");
    }
}
Output:

Case-1
Enter the value of x1 coordinate of A:
4
Enter the value of y1 coordinate of A:
4
Enter the value of x1 coordinate of B:
4
Enter the value of y1 coordinate of B:
8
Enter the value of x1 coordinate of C:
8
Enter the value of y1 coordinate of C:
8
Enter the value of x1 coordinate of D:
8
Enter the value of y1 coordinate of A:
4
Valid square

Case-2
Enter the value of x1 coordinate of A:
1
Enter the value of y1 coordinate of A:
2
Enter the value of x1 coordinate of B:
3
Enter the value of y1 coordinate of B:
4
Enter the value of x1 coordinate of C:
5
Enter the value of y1 coordinate of C:
6
Enter the value of x1 coordinate of D:
7
Enter the value of y1 coordinate of A:
8
Not a valid square

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.

Related Java Articles:

Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array

Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array

In the previous article, we have seen Java Program to Delete All 0 Element Values from an Array of Integers

In this article we will see how to find number of elements greater than a given number in a given sub array using Java programming language.

Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array

Prerequisite: 

See below articles to know more about Array, array declaration, array instantiation and array initialization.

Let’s see different ways how to find number of elements greater than a given number in a given sub array.

Method-1: Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array By Static Initialization of Array Elements

Approach:

  • Declare an array say arr[] with numbers as array elements.
  • Enter a specific number and store it in an integer variable say int number which will be used for comparison.
  • Declare two integer variables say start and end and assign the start index and end index value to it by taking user input.
  • Then compare the specific number with all the elements of the sub array.
  • If any sub array element is greater than that specific number then print it as well as keep track on number of sub array elements which are greater than that specific element by using an integer variable say count.
  • At last print the value of count as well.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        //array declared
        int arr[]={4,6,1,8,2,9,7,3,5};
        
        // Displaying the original array
        System.out.print("Original array: ");
        //printing the array
        for(int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        //Asking the user to take input of a number
        //We will find sub array elements which are greater than this number
        System.out.print("Enter a specific number: "); 
        int number=sc.nextInt();
         
        //Entering the start and end index value for sub array
        System.out.print("Enter sub array start index: "); 
        int start=sc.nextInt();
        System.out.print("Enter sub array end index: "); 
        int end=sc.nextInt();
        
         // Displaying the sub array
        System.out.print("Sub array: ");
        //printing the array
        for(int i = start; i <= end; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        
        System.out.print("The numbers which are greater than "+number+" are:");
        //count value initialized as 0
        //to keep track on number of elements which are greater than the specific number.
        int count=0;
        for(int i = start; i <= end; i++)
        {
            if(arr[i]>number)
            {
                System.out.print(arr[i]+" ");
                count++;
            }
        }
        System.out.println();
        
        System.out.print("So, total "+count+" elements are greater than "+number+" in the given sub array");
    }
}
Output:

Original array: 4 6 1 8 2 9 7 3 5 
Enter a specific number: 8
Enter sub array start index: 1
Enter sub array end index: 6
Sub array: 6 1 8 2 9 7 
The numbers which are greater than 8 are:9 
So, total 1 elements are greater than 8 in the given sub array

Method-2: Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask user for input of array length.
  • Initialize the array with given size.
  • Ask the user for input of array elements.
  • Display the original array arr[].
  • Enter a specific number and store it in an integer variable say int number which will be used for comparison.
  • Declare two integer variables say start and end and assign the start index and end index value to it by taking user input.
  • Then compare the specific number with all the elements of the sub array.
  • If any sub array element is greater than that specific number then print it as well as keep track on number of sub array elements which are greater than that specific element by using an integer variable say count.
  • At last print the value of count as well.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int arr[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        for (int i = 0; i < num; i++) 
        { 
        arr[i] = sc.nextInt(); 
        }
        
        // Displaying the original array
        System.out.print("Original array: ");
        //printing the array
        for(int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        //Asking the user to take input of a number
        //We will find sub array elements which are greater than this number
        System.out.print("Enter a specific number: "); 
        int number=sc.nextInt();
         
        //Entering the start and end index value for sub array
        System.out.print("Enter sub array start index: "); 
        int start=sc.nextInt();
        System.out.print("Enter sub array end index: "); 
        int end=sc.nextInt();
        
         // Displaying the sub array
        System.out.print("Sub array: ");
        //printing the array
        for(int i = start; i <= end; i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        
        
        System.out.print("The numbers which are greater than "+number+" are:");
        //count value initialized as 0
        //to keep track on number of elements which are greater than the specific number.
        int count=0;
        for(int i = start; i <= end; i++)
        {
            if(arr[i]>number)
            {
                System.out.print(arr[i]+" ");
                count++;
            }
        }
        System.out.println();
        
        System.out.print("So, total "+count+" elements are greater than "+number+" in the given sub array");
    }


}
Output:

Enter the number of elements in the array: 6
Enter the elements: 1 2 3 4 5 6
Original array: 1 2 3 4 5 6 
Enter a specific number: 4
Enter sub array start index: 2 
Enter sub array end index: 5
Sub array: 3 4 5 6 
The numbers which are greater than 4 are:5 6 
So, total 2 elements are greater than 4 in the given sub array

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 Articles:

Java Program to Check if a Line Touches or Intersects a Circle

In the previous article, we have seen Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

In this article we will discuss about how to Check if a Line Touches or Intersects a Circle using Java programming language.

Java Program to Check if a Line Touches or Intersects a Circle

Before jumping into the program directly, let’s first know how can we Check if a Line Touches or Intersects a Circle .

Explanation:

To Check if a Line Touches or Intersects a Circle:

  1. Find the perpendicular distance between center of circle and given line.
  2. Compare this distance with the radius.
    1. If perpendicular > radius, then line lie outside the circle.
    2. If perpendicular = radius, then line touches the circle.
    3. If perpendicular < radius, then line intersect the circle.

Let’s see different ways to to check if a line touches or intersects a circle.

Method-1: Java Program to Check if a Line Touches or Intersects a Circle By Using Static Value

Approach:

  • Declare the value for ‘a’, ‘b’ and ‘c’, coordinates of the center and size of the radius.
  • Then calculate the perpendicular distance between the center of the circle and the line.
  • Compare the perpendicular distance with radius and print the result.

Program:

import java.awt.Point; 
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args)
    {
        //Static initialization of the line, center coordinates and the radius
        int a = 1, b = 1, c = -16;
        Point rad = new Point(0,0);
        double radius = 5;
        // Caclculates the distance between the center and the line
        double distance = (abs(a*rad.x+b*rad.y+c))/sqrt(a*a+b*b);
        // Prints the result
        if(radius==distance)
            System.out.println("The line the touches the circle");
        else if(radius>distance)
            System.out.println("The line the intersects the circle");
        else if(radius<distance)
            System.out.println("The line is outside the circle");
    }
} 
Output:

The line is outside the circle

Method-2: Java Program to Check if a Line Touches or Intersects a Circle By User Input Value

Approach:

  • Take user input of value for ‘a’, ‘b’ and ‘c’, coordinates of the center and size of the radius.
  • Then calculate the perpendicular distance between the center of the circle and the line.
  • Compare the perpendicular distance with radius and print the result.

Program:

import java.awt.Point; 
import java.util.*;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        //Asking the user to input the line, center coordinates and the radius
        System.out.println("Enter a, b and c");
        int a = scan.nextInt(), b = scan.nextInt(), c= scan.nextInt();
        System.out.println("Enter coordinates of the radius");
        Point rad = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter the radius");
        double radius = scan.nextDouble();
        
        // Caclculates the distance between the center and the line
        double distance = (abs(a*rad.x+b*rad.y+c))/sqrt(a*a+b*b);
        
        // Prints the minimum revloutions
        if(radius==distance)
            System.out.println("The line the touches the circle");
        else if(radius>distance)
            System.out.println("The line the intersects the circle");
        else if(radius<distance)
            System.out.println("The line is outside the circle");
    }
}
Output:

Case-1
Enter a, b and c
1 -1 0
Enter coordinates of the radius
0 0
Enter the radius
5
The line the intersects the circle

Case-2
Enter a, b and c
20 25 30
Enter coordinates of the radius
10 11
Enter the radius
9
The line is outside the circle

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Articles:

Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

In the previous article, we have seen Java Program to Solve Pizza Cut Problem(Circle Division by Lines)

In this article we will discuss about how to find minimum revolutions to move center of a circle to a target using java programming language.

Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

Before jumping into the program directly, let’s first know how can we find minimum revolutions to move center of a circle to a target .

Explanation:

Formula to Find Minimum Revolutions to Move Center of a Circle to a Target: ceil(d/2*r)

Example:

When r=2,P1=(0,0), and P2=(0,4), d = 4

Minimum Revolutions: ceil(d/2*r)

=> ceil(4/2*2)

=>ceil(1)

=> 1

Let’s see different ways to find minimum revolutions to move center of a circle to a target.

Method-1: Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target By Using Static Value

Approach:

  • Declare the value for the coordinates of the point, of radius and size of radius.
  • Find the distance between both the points.
  • Find the minimum revolutions using the formula ceil(distance/(2*radius))
  • Then print the result.

Program:

import java.awt.Point; 
import java.util.Scanner;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args){
        // Static initialization of both points and the radius
        Point rad = new Point(0,0);
        Point p = new Point(0,4);
        double radius = 2;
        // Caclculates the distance between the radius and the point
        double distance = Math.sqrt((rad.x-p.x)*(rad.x-p.x)+(rad.y-p.y)*(rad.y-p.y));
        // Prints the minimum revloutions
        System.out.println("The minimum revolutions required is "+(int)Math.ceil(distance/(2*radius)));
    }
}
Output:

The minimum revolutions required is 1

Method-2: Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target By User Input Value

Approach:

  • Take user input the value for the coordinates of the point and radius and size of radius.
  • Find the distance between both the points.
  • Find the minimum revolutions using the formula ceil(distance/(2*radius))
  • Then print the result.

Program:

import java.awt.Point; 
import java.util.Scanner;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        //Asking the user to input both points and the radius
        System.out.println("Enter coordinates of the point");
        Point p = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter coordinates of the radius");
        Point rad = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter the radius");
        double radius = scan.nextDouble();
        // Caclculates the distance between the radius and the point
        double distance = Math.sqrt((rad.x-p.x)*(rad.x-p.x)+(rad.y-p.y)*(rad.y-p.y));
        // Prints the minimum revloutions
        System.out.println("The minimum revolutions required is "+(int)Math.ceil(distance/(2*radius)));
    }
}
Output:

Enter coordinates of the point
5 5
Enter coordinates of the radius
3 3 
Enter the radius
2
The minimum revolutions required is 1

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 Articles:

Java Program to Solve Pizza Cut Problem(Circle Division by Lines)

Java Program to Solve Pizza Cut Problem(Circle Division by Lines)

In the previous article, we have seen Java Program to Check Whether a Point Exists in Circle Sector or Not

In this article, we will learn how to solve the pizza cut problem using java program language.

Java Program to Solve Pizza Cut Problem(Circle Division by Lines)

In this problem, there is given the number of cuts on pizza in input, our task is to find the number of pieces of pizza that will be present after cutting.

The number of pieces can be find out simply by using a Formula.

Maximum number of pieces = 1 + n*(n+1)/2

Where,

  • n = number of cuts

Let’ see different ways to solve pizza cut problem.

Method-1: Java Program to Solve Pizza Cut Problem(Circle Division by Lines) By Using Static Input Values

Approach

  1. A simple approach to counting pieces is by using the above-mentioned formula.
  2. Declare an integer variable say ‘n‘ which holds the value of number of cuts.
  3. Display the result by calling the countMaximumPieces() method.
  4. Print the result.

 Program:

class Main 
{
    // main method
    public static void main(String arg[]) 
    {
        int n=5;
        //calling the user defined method
        //and pasing number of cuts as paramater
        countMaximumPieces(n);
    }
    
    // Function for finding maximum pieces
    // with n cuts.
    public static void countMaximumPieces(int n) 
    {
        int pieces= 1+n*(n+1)/2; 
        System.out.print("Maximum no. of pieces:"+ pieces);
    }
}
Output:

Maximum no. of pieces:16

Method-2: Java Program to Solve Pizza Cut Problem(Circle Division by Lines) By Using User Input Values

Approach

  1. A simple approach to counting pieces is by using the above-mentioned formula.
  2. Declare an integer variable say ‘n‘ which holds the value of number of cuts.
  3. Take the user input of value of n.
  4. Display the result by calling the countMaximumPieces() method.
  5. Print the result.

 Program:

import java.util.*;

class Main 
{
    // main method
    public static void main(String arg[]) 
    {
        //scanner class object created
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number of cuts:");
        int n=sc.nextInt();
        //calling the user defined method
        //and pasing number of cuts as paramater
        countMaximumPieces(n);
    }
    
    // Function for finding maximum pieces
    // with n cuts.
    public static void countMaximumPieces(int n) 
    {
        int pieces= 1+n*(n+1)/2; 
        System.out.print("Maximum no.of pieces:"+ pieces);
    }
}
Output:

Enter number of cuts:5
Maximum no.of pieces:16

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

Related Java Articles:

Java Program to Check Whether a Point Exists in Circle Sector or Not

Java Program to Check Whether a Point Exists in Circle Sector or Not

In the previous article, we have seen Java Program to Check If Given Four Points Form a Square

In this article, we will check whether a point exists in a circle sector or not by using Java programming language.

Java Program to Check Whether a Point Exists in Circle Sector or Not

Before jumping into the program directly, let’s first know how to check whether a point exists in a circle sector or not.

Explanation:

Suppose, we have a circle centered at coordinates (0,0), the starting angle of the circle sector and the size of the circle sector in percentage.

Then we need to find out polar coordinates of that point and then go through the following steps:

  1. Converting x, y to polar coordinates.
  2. Check polarradius is less then radius of circle.
  3. Angle is between startAngle and endAngle.

Note : We can calculate by using Formulas  –

  • EndingAngle = 360 / percentage + StartingAngle ;
  • double polarradius = Math.sqrt(x*x+y*y);
  • double Angle = Math.atan(y/x);

Approach:

  1. Create checkPoint() method and find the endAngle, polarRadius, and Angle using the above formulas.
  2. Check the condition if it is true which means points exist in the circle else do not exist.
  3. Call the checkPoint() method in the main() method and display the output.

 Program:

class Main
{ 
    //main method
    public static void main(String arg[]) 
    { 
        int radius = 8;
        int x = 3;
        int y = 4; 
        float startAngle = 0; 
        float percent = 12;
        //calling the checkPoint() method
        checkPoint(radius, x, y, percent, startAngle); 
    }
    
    //user defined method
    static void checkPoint(int radius, int x, int y, float percent, float startAngle) 
    { 
      
        //find endAngle 
        float endAngle = 360/percent + startAngle; 
       
        //find polar co-ordinates 
        double polarRadius = Math.sqrt(x*x+y*y); 
        double Angle = Math.atan(y/x); 
       
        // Checking whether polarradius is less then radius of circle or not 
        // and checking whether Angle is between startAngle and endAngle or not 
        if (Angle>=startAngle && Angle<=endAngle && polarRadius<rad) 
        
            System.out.print("Point"+"("+x+","+y+")"+ 
            " exist in the circle sector"); 
        else
        
            System.out.print("Point"+"("+x+","+y+")"+ 
            " exist in the circle sector"); 
    } 
}
Output:

Point(3,4) exist in the circle sector

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.

Related Java Articles: