Tech Study

what is n in java

Introduction:

n in java

Here’s a Java program to calculate the sum of n in java

1 + 1/2 + 1/3 + 1/4 + ... + 1/n

import java.util.Scanner;




public class SeriesSum {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the value of n: ");

        int n = input.nextInt();




        double sum = 0;

        for (int i = 1; i <= n; i++) {

            sum += 1.0 / i;

        }




        System.out.println("The sum of the series is: " + sum);

    }

}

n in java

In this program we first use the value of n in java as input the user which is using the scanner class. After that we declare a variable sum to store of the series and initialize it to 0

Then we use loop for iterate of 1/ I to ensure that the division is done using double precision.

After all we print the sum of the series using System.out.println().

For using these program, we can compile it a java compiler such a java and the run it using the java virtual machine {jvm}

Java series sum

When the program is run , it will prompt you to enter the value of n . we can enter any positive integer value of n the program will calculate the sum of series by using formula motioned in the above article

For example when you enter 5 as the value of n the program will output :

Enter the value of n:5

The sum of the series in n in java is : 2.283333333333333

Which means that the sum of the series : 1 + 1/2 + 1/3 + 1/4 + 1/5 is approximately equal to 2.283333333333333.

This how we can find n in java

TaggedWrite a java program to calculate the sum of following series where n is input by user

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more