Question
import java.util.Scanner; public class part4 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int targetNumber,targetCount=0,oddCount=0,count=0; int max=Integer.MIN_VALUE; System.out.print(Enter your target number:
import java.util.Scanner; public class part4 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int targetNumber,targetCount=0,oddCount=0,count=0; int max=Integer.MIN_VALUE; System.out.print("Enter your target number: "); targetNumber=input.nextInt(); System.out.print("Enter an integer value (0 to quit): "); int value = input.nextInt(); int sum = 0; while (value != 0) { sum += value; count++; if(value%2!=0) oddCount++; if(value>max) max=value; if(value==targetNumber) targetCount++; System.out.print("Enter next value (0 to quit): "); value = input.nextInt(); } System.out.println(" The sum of the input values is: "+sum); System.out.println("The number of inputs is: "+count); if(count!=0) System.out.printf("The average of the input values is: %.2f ",(float)sum/count); else System.out.println("The average of the input values is: average undefined"); System.out.println("The number of odd values input is: "+oddCount); System.out.println("The value "+targetNumber+" was input "+targetCount+" times"); System.out.println("The maximum value entered was: "+max); } }
You are to write a program that generates the second, third, and fourth powers of a list of
whole numbers from 1 to n where n is input by the user. Write a Java program to do this.
First, ask the user for the largest of the whole numbers to use (n). Second, output the
column headers (see below, n, n^2, n^3, n^4). We can use the \t tab escape character to
line things up. As in :System.out.println(n \t\t n^2 \t\t n^3 \t\t n^4);
Next, use a for-loop to iterate from 1 to n, that is, write a counting for-loop that starts at 1
and counts to the users input n. Each time through the loop compute the four powers for
the current value of your loop counter variable. Once you have calculated all 4 powers,
print them out separated by two tabs so that they line up with the headers printed prior to
the loop or you can use System.out.printf to line arrange the output in columns. The sample
below assumes that the user entered the value 5 for n.
Test your program with different input values such as 5, 10, 1, 0 and -1. For 0 and -1, you
should get no numeric output at all. If you wrote your loop correctly, that will be the case.
Debugging tip:
If you get incorrect results when running code that contains a loop, place
some print statements inside the loop to print the values of any variables that you think
might be involved in the errors. This helps you check what your loop is actually doing.
----------
Write one additional program which itself contains two loops. You will have to decide
which loop type to use and how to write each one. Both loops will have a loop body that
consists of a computation and an output.
Loop 1: sum up all of the values from 1 to 10, outputting the sum (and only the
sum) as you go. For instance, it will output 1, 3, 6, 10, etc (on separate lines).
Loop 2: sum up all of the values of Integers starting at 1 and continuing until the
sum is greater than 200, again outputting results as you go.
It is up to you to decide what type of loops to use and how to write each, and what other
variables and instructions are needed in your program.
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