Add Euler Mascheroni constant to each element of an array in java

The Euler-Mascheroni constant is denoted by gamma, it is a mathematical constant approximately equal to 0.57721.It appears frequently in various areas of mathematics and it arises naturally in the study of harmonic series and certain integrals in calculus .

How to add Euler-Mascheroni constant to each element of an array in java

Here are the steps :-

Step-1:-Understand Euler-Mascheroni constant
The Euler-Mascheroni constant is denoted by gamma, it is a mathematical constant and it arises naturally in the study of harmonic series and certain integrals in calculus.

Step-2:-Create an array
First you need to create an array of numbers in java.

double[] numbers={1.0,2.0,3.0};

Step-3:-Add Eulers-Mascheroni constant to each element
Iterate through the array and add the Eulers-Mascheroni constant to each element.

double eulermascheroniconstant=0.57721;
for(int i=0;i<numbers.length;i++){
    numbers[i]+=eulersmascheroniconstant;
}

Step-4:-Display the result

System.out.println("array after adding Euler-Mascheroni constant:");
for(double num:numbers){
    System.out.println(num);
}

Here is the complete code:

public class main{
   public static void main(String[] args){
      double[] numbers={1.0,2.0,3.0};
      double eulerMascheroniConstant=0.57721;
      for(int i=0;i<numbers.length;i++){
          numbers[i]+=eulerMascheeroniConstant;
      }
      System.out.println("Array after adding Euler-Mascheroni constant:");
      for (double num:numbers){
          System.out.println(num);
      }
   }
}

Here is the expected output of the above code:

Array after adding Euler-Mascheroni constant:

1.57721
2.57721
3.57721
Each element of the array has been incremented by the Euler-Mascheroni constant.

Explanation :

1.It is defined as private static final double Euler-Mascheroni -constant=0.5772166490153286060.This ensures the value is available throughout the class.

2.method to add constant:

  • This method takes an array of doubles as input and returns a new array where the constant has been added to each element.
  • A new array ‘resultArray’ of the same length should be created as the input to store the results.
  • A ‘for’ loop iterates through each element of the input array.

3.Main Method:

  • example program is derived below.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top