I'm in a beginner Java class at school and I'm having a hard time getting this to work.
Most of our problems require JOptionPane and for or while loops and if statements and those kinds of things...
For #6, I'm thinking about using a while loop and a for loop inside of that and then I'll use the mod function to make sure that a prime number is a number with no remainder...
For #7 and #8, I honestly have no idea how to start... I've tried looking up examples on the internet... but I'm still having a hard time putting it all together.
Thank you for anyone that's willing to help!!
6.
Write a program that repeatedly prompts the user to enter a positive integer > 1. The program should then take this integer and determine whether it is prime. (A prime integer is evenly divisible only by itself and 1.) Then display whether the integer is prime or composite (not prime). If the user enters 0, then exit from the program.
Hint: Use a for loop and the mod function.
7.
Consider the sum 1/2 + 1/4 + 1/8 + …. The first term of this sum is 1/2 = .5. The second term is 1/2 + 1/4 = .75. The third term is 1/2 + 1/4 + 1/8 = .875. Create a program that repeatedly prompts the user for a positive integer, n. Then have the program calculate the nth term of the above sum as a decimal and display it to the user. For example, if the user enters 4, then your program must compute 1/2 + 1/4 + 1/8 + 1/16 = .9375. If the user enters 0, then exit from the program.
Hint: Use a for loop.
8.
This problem is like problem 7, but instead of asking the user to enter an integer, n, we ask the user to enter a decimal, d, greater than 0 and less than 1. We then calculate what n is for the first term that causes the sum to be larger than d. Display this value of n to the user. For example, if the user enters .9 for d, then we should display 4 for n, since term 4 is the first term making the sum larger than .9. (Term 3 is only .875, which is less than .9)
Hint: Use a while loop.