Question
In this project I want you to do two tasks. These two tasks should be done in their own methods, both of which would be
In this project I want you to do two tasks. These two tasks should be done in their own methods, both of which would be called from your main method.
Suppose you want to calculate the following series:
1 + 1/2 + 1/3 + ... + 1/n
Write a program that computes the results of the summation of the preceding series from left and right for some n entered by the user and outputs the result. Then output the result from summing it from left to right.
Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin
primes, 5 and 7 are twin primes and 11 and 13 are twin primes. Write a program to find all twin
primes less than 1000. Display the output as follows:
(3,5)
(5,7)
Below is the java code that I've written for this and I can't figure out why it keeps giving me an error message, can you spot the error?
import java.io.*;
import java.util.Scanner;
public class Main { public static void main(String[]args) { double totalSum = sum; //calling the sum method to show results System.out.printf("Sum is ", totalSum(num)); for (int i=2; i<1000; i++) //calling the prime function to output the results. { if(prime(i) && prime(i+ 2)) { System.out.printf("(%d, %d) ", i, i + 2); } } }
public static double sums(int num) { System.out.print("Enter a number: "); Scanner input = new Scanner (System.in); int num = input.nextDouble(); if(num > 0) { double sum = 0; for(double i=1; i<=num; i++) { sum = 1/i + sum; return sum; } } else { System.out.printIn("Invalid input"); } return result; }
public static boolean prime(int n) { if (n<2) return false; for (int i=2; i<=n/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