Write a static method called printSequenceTo that takes a target value as a parameter and then prints terms from a particular numerical sequence until they add up to a value greater than or equal to the target and that returns the number of terms that were included. For example, if the following calls are made:
int n1 = printSequenceTo(3.0); int n2 = printSequenceTo(5.5);
The following output should be produced:
1/2 + 2/3 + 3/4 + 4/5 + 5/6 = 3.5500000000000003 1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746
The variable n1 is set to 5 because it took 5 terms from the sequence to get a sum that is at least 3.0. The variable n2 would be set to 8 because it took 8 terms to get a sum that is at least 5.5. You are to exactly reproduce the format of this output. Notice that the sum is not rounded.
Java Code:
class Main {
public static int printSequenceTo(double n)
{
int count = 1;
double sum = ((double)count/(count+1));
System.out.print(count + "/" + (count+1));
while(sum < n)
{
count++;
sum += ((double)count/(count+1));
System.out.print(" + " + count + "/" + (count+1));
}
System.out.println(" = " + sum);
return count;
}
public static void main(String[] args) {
int n1 = printSequenceTo(3.0);
System.out.println("Sequence took " + n1 + " terms");
int n2 = printSequenceTo(5.5);
System.out.println("Sequence took " + n2 + " terms");
}
}
OUTPUT:

Write a static method called printSequenceTo that takes a target value as a parameter and then...
Write a method, name it sumNums. This method should compute and return the sum of integers from n1 to n2. For example, sum of integers sumNums(1,10) will sum the sequence 1+2+3+4+5+6+7+8+9+10
1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...
C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In:"); int nn = inp.nextInt(); int n0, n1, n2, n3, n4; //Change only the code below //Set n0, n1, . . . n4 to each digits in nn by replacing //the blanks _A_, _B_, ect //Hint: the ones digit of 1234 is 1234%10 // the hundredth digit of 31415 is (31415/100)%10 n0 = _A_; n1 = _B_; n2 = _C_; n3 = _D_;...
Description The purpose of this challenge is to various flow looping and control structures. This challenge displays a menu and asks the user to perform various tasks. Requirements Show a menu to the user with the following choices: a, b, c, q (Show the menu once and use a do-while statement to prompt the user for a menu command): a – Display all odd numbers between n1 and n2. Ask the user to enter two numbers, n1 and n2, using...
Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....
The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...
Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will receive a stack of "int" and output the sum of the squares of the elements in the stack. b- int plus_minus_rec(stack<int> stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c- void prt_chars_rev_rec(stack<char>...
Write a full C++ program Assignment4_SequenceSum that prints terms of the following mathematical sequence: ( also written as ) Your program should accept a real number from the user representing a limit, and should add and print terms of the sequence until the sum of terms meets or exceeds that limit. For example, if your user enters 2.0, program prints terms until the sum of those terms is at ≥ 2.0. You should round your answer to...
Write a method called printReverse() that
takes a string and uses recursion to print the contents of the
string in reverse order. The string itself should
not be reversed; it must be left in its
original form.
The method has the following header:
void printReverse(String s, int i)
where s is a reference to the string, and i is an integer
parameter that you may use as you see fit. You do not need
to code up this method as...