Question

In: Computer Science

Write a method sumTo that accepts an integer parameter n and returns the sum of the...

Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals.

In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n

For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0.

Include a loop.

Please help for Java programming.

Solutions

Expert Solution

If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.

Solution :

Code:

import java.util.*;
public class HelloWorld
{
// function sumTo
public static float sumTo(int n)
{
// initialize sum as 0
float sum=0;
// if n is less than 0 then return -1
if (n<0)
{
return -1;
}
// otherwise run a loop from 1 to n
else
{
for(int i=1;i<=n;i++)
{
// add 1/i in each iteration
sum = sum + (1/(float)i);
}
// return the sum
return sum;
}
}
//main function
public static void main(String []args)
{
// make an object of Scanner class
Scanner sc = new Scanner(System.in);
// input an integer
int n = sc.nextInt();
// call the function
float res = sumTo(n);
// if the res is -1 then print error
if (res==-1)
{
System.out.println("Error");
}
// otherwise print the value of res
else
{
System.out.println(res);
}
}
}

Output :


Related Solutions

- Write a method sum that expects a List<Integer> as a parameter. The method returns an...
- Write a method sum that expects a List<Integer> as a parameter. The method returns an int representing the sum of the integers in the list. - Write an index-based loop that prints the contents of a list.
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it...
a) Write a function drawShape() that accepts a parameter n, and: If n is odd, it constructs a pattern of a diamond of height n If n is even, it constructs an hourglass of length n b) What is the time complexity of the drawShape() function you created? C++ language with for loop
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper...
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper factors/divisors ofn; for example,the sum of factorsof 20= 10+5+4+2 = 21.Note that 1 is nota properfactor of any number.Sample command => output:(display (sumOfFactors20))=> 21Hint: Calla recursivefunction sum_helperfrom sumOfFactorsfunction as follows:(define (sumOfFactors n)(sum_helper n (-n 1)))Here sum_helperfunction takes two parameters: n, d and returns the sum of all factors of n whichare <= d; for e.g. (sum_helper 20 6) returnsall proper factors of 20 which...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT