📜  产生谐波序列的Java程序

📅  最后修改于: 2022-05-13 01:54:50.209000             🧑  作者: Mango

产生谐波序列的Java程序

调和级数是等差数列的倒数。一般来说,谐波级数中的项可以表示为

其中h是调和级数, a是等差数列, d是等差数列之间的公差, n是第 n 项。

示例 1:(使用 while 循环)

Java
// Java Program to Generate Harmonic Series
 
class HarmonicSeries {
 
    // this is a main function
    public static void main(String args[])
    {
 
        // num is the number of values we want in a series
        int num = 5;
        double result = 0.0;
 
        System.out.println("The harmonic series is: ");
 
        // printing the harmonic series till num value
        // using while loop
        while (num > 0) {
 
            // calculating harmonic values
            result = result + (double)1 / num;
 
            // after calculating harmonic value
            // decrementing num by 1 which means the common
            // difference is 1
            num--;
            System.out.print(result + ", ");
        }
    }
}


Java
// Java Program to Generate Harmonic Series
 
class HarmonicSeries {
 
    // this is a main function
    public static void main(String args[])
    {
 
        // num is the number of values we want in a series
        int num = 5;
        double result = 0.0;
 
        System.out.println("The harmonic series is: ");
 
        // printing the harmonic series till num value
        // using for loop
        for (int i = num; i > 0; i--) {
 
            // calculating harmonic values
            result = result + (double)1 / i;
            System.out.print(result + ", ");
        }
    }
}


Java
// Java Program to Generate Harmonic Series
 
// importing necessary java packages
import java.util.Scanner;
import java.lang.*;
 
class HarmonicSeries {
 
    // this is a main function
    public static void main(String args[])
    {
 
        // scanner class is a pre-defined class in java
        // for taking input from keyboard
        Scanner in = new Scanner(System.in);
 
        System.out.print("Enter Number: ");
 
        // storing input value in num
        int num = in.nextInt();
        double result = 0.0;
 
        System.out.println("The harmonic series is: ");
 
        // printing the harmonic series till num value
        // using for loop
        for (int i = num; i > 0; i--) {
 
            // calculating harmonic values
            result = result + (double)1 / i;
 
            System.out.print(result + ", ");
        }
    }
}


输出
The harmonic series is: 
0.2, 0.45, 0.7833333333333333, 1.2833333333333332, 2.283333333333333,

示例 2:(使用 for 循环)

Java

// Java Program to Generate Harmonic Series
 
class HarmonicSeries {
 
    // this is a main function
    public static void main(String args[])
    {
 
        // num is the number of values we want in a series
        int num = 5;
        double result = 0.0;
 
        System.out.println("The harmonic series is: ");
 
        // printing the harmonic series till num value
        // using for loop
        for (int i = num; i > 0; i--) {
 
            // calculating harmonic values
            result = result + (double)1 / i;
            System.out.print(result + ", ");
        }
    }
}
输出
The harmonic series is: 
0.2, 0.45, 0.7833333333333333, 1.2833333333333332, 2.283333333333333,

示例 3:

Java

// Java Program to Generate Harmonic Series
 
// importing necessary java packages
import java.util.Scanner;
import java.lang.*;
 
class HarmonicSeries {
 
    // this is a main function
    public static void main(String args[])
    {
 
        // scanner class is a pre-defined class in java
        // for taking input from keyboard
        Scanner in = new Scanner(System.in);
 
        System.out.print("Enter Number: ");
 
        // storing input value in num
        int num = in.nextInt();
        double result = 0.0;
 
        System.out.println("The harmonic series is: ");
 
        // printing the harmonic series till num value
        // using for loop
        for (int i = num; i > 0; i--) {
 
            // calculating harmonic values
            result = result + (double)1 / i;
 
            System.out.print(result + ", ");
        }
    }
}

输出

$ javac HarmonicSeries.java
$ java HarmonicSeries

Enter Number: 5
The harmonic series is: 
0.2, 0.45, 0.7833333333333333, 1.2833333333333332, 2.283333333333333
$ javac HarmonicSeries.java
$ java HarmonicSeries

Enter Number: 6
The harmonic series is: 
0.16666666666666666, 0.3666666666666667, 0.6166666666666667, 0.95, 1.45, 2.45