Question
I am trying to write a program that in one method calculates the sum of 1+1/2+1/3+...1/n. The n value is entered by the user. Another
I am trying to write a program that in one method calculates the sum of 1+1/2+1/3+...1/n. The n value is entered by the user. Another method will find twin primes under 1000. Below I attached the code I have so far. I cannot figure out the errors. What are my errors?
import java.io*; import java.util.Scanner;
public class Main { public static void main(String[] args) { System.out.println ("Enter a positive value for n: "); Scanner input = new Scanner (System.in); double n = input.nextDouble(); double sum; if(n < 0) { System.out.println("Negative input."); } else { sum = calculate(n); System.out.printf("The summation is: ", sum); } public static void main(String[] args) { for(int i = 2; i <=1000; i++) { if (twinPrime(i) && twinPrime(i + 2)) { sytem.out.println("(p,p) ", i, i + 2); } } } }
public static double calculate (double n) { double total = 0.0; for(double i=1; i<=n; i++ ) { total = total + 1/i; } return total; }
public static boolean twinPrime(int number) { if (number == 1) return false; for(int i = 2; i <= number/2; i++) { if(n % i == 0) return false; } return true; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started