Question

Write a code segment as specified below. // 1. Prompt the user to enter a positive...

Write a code segment as specified below.

// 1. Prompt the user to enter a positive integer number (say n).

// 2. Calculate the sum of all the reciprocals of the numbers from 1 to n.

For example, sum = (1 / 1) + ( 1 / 2 ) + ( 1 / 3) + ( 1 / 4 ) + … + ( 1 / n )

// 3. Print out the integer number and the sum.

• NOTE: Each reciprocal should not truncate decimal point numbers (i.e., 1 / 2 == 0.5, 1 / 4 == 0.25, etc.). Recall cast operator(s).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Tester {

      public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);

            //Asking the user to enter n

            System.out.print("Enter n: ");

            int n = scan.nextInt();

            double sum =0;

            //Calculating the sum of reciprocals

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

                  double temp = (double)1/i;

                  sum += temp;

            }

            //Printing out the result

            System.out.print("Sum = "+sum);

            scan.close();

      }

}

output:

Add a comment
Know the answer?
Add Answer to:
Write a code segment as specified below. // 1. Prompt the user to enter a positive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming For this task, you will have to write a program that will prompt the...

    C Programming For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature: int sum(int* arr, int n); Your code should look something like this (you can start with this as a template: int sum(int* arr, int...

  • Matlab write a code that - Prompt a user to enter 2 positive numbers (check the...

    Matlab write a code that - Prompt a user to enter 2 positive numbers (check the that they are positives) - find the LCM - For each number test its multiplicity of 2 , 3 , 5 , 7 , 11

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • In C code prompt and read from the user an integer number, 'N', of double precision...

    In C code prompt and read from the user an integer number, 'N', of double precision floating point numbers to allocate for the purpose of the current test run. Dynamically allocate an array of N double precision floating point numbers; i.e. the array will require "N*sizeof(double)" bytes of RAM for storage, call this M.

  • Write a complete program in assembly line that:    1. Prompt the user to enter 10...

    Write a complete program in assembly line that:    1. Prompt the user to enter 10 numbers.    2. save those numbers in a 32-bit integer array.    3. Print the array with the same order it was entered.    3. Calculate the sum of the numbers and display it.    4. Calculate the mean of the array and display it.    5. Rotate the members in the array forward one position for        9 times. so the last...

  • Please solve it by Python 3 Write a program that asks the user for a positive...

    Please solve it by Python 3 Write a program that asks the user for a positive integer limit and prints a table to visualize all factors of each integer ranging from 1 to limit. A factor i of a number n is an integer which divides n evenly (i.e. without remainder). For example, 4 and 5 are factors of 20, but 6 is not. Each row represents an integer between 1 and 20. The first row represents the number 1,...

  • in java Write an application called Squaring that will: • Prompt the user to enter an...

    in java Write an application called Squaring that will: • Prompt the user to enter an integer greater than 1. Your code for this should be type-safe and validate the number, meaning that your code should re-prompt the user for another entry if they enter anything other than an integer that is greater than 1. • Repeatedly square the integer until it exceeds 1 million, outputting the value each time after squaring. • Output the number of squarings required to...

  • Write a program that will loop ten times. In each iteration prompt user to enter an...

    Write a program that will loop ten times. In each iteration prompt user to enter an integer between -50&50. Print the input, keep a running sum of the inputs, and track the minimun and maximum numbers input. After the loop is complete, minimum number entered, maximum number entered, sum of all numberes entered, and average of all numbers entered. Format all output. Before running the loop, print the label "Input values: ". Within the loop, print value entered by user...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT