Computer >> Computer tutorials >  >> Programming >> Java

Java Program to get number of elements with odd factors in given range


To get number of elements with odd factors in given range, the Java code is as follows −

Example

import java.io.*;
import java.util.*;
import java.lang.*;
public class Demo{
   public static int square_count(int low_range, int high_range){
      return (int)Math.pow((double)high_range,0.5) - (int)Math.pow((double)low_range-1,0.5);
   }
   public static void main (String[] args){
      int low_range = 55, high_range = 1000;
      System.out.print("The number of values with odd factors between a given range of numbers
      is : " + square_count(low_range, high_range));
   }
}

Output

The number of values with odd factors between a given range of numbers is : 24

A class named Demo contains a function named ‘square_count’. This function is defined by passing two integer values as parameters. It returns the number of elements that have odd factors given a specific range. This is done by using the math function ‘pow’. In the main function, the lower range and higher range values are defined and the function ‘square_count’ is called with the lower and higher range values. The relevant message is displayed on the console.