Explanation of Solution
Given code:
//Declare the variables
int x = 1, total;
//Execute "while" loop
while (x <= 10)
{
//Perform calculation
total += x;
//Increment "x" by "1"
++x;
}
Error:
In the given code, the variable “total” is not initialized to perform the calculation.
Corrected code:
//Declare and initialize the variable
int x = 1, total=0;
//Execute "while" loop
while (x <= 10)
{
//Per...
Explanation of Solution
Given code:
//Execute "while" loop
while (x <= 100)
//Perform calculation
total += x;
//Increment "x" by "1"
++x;
Error:
In the given code, the body of “while” is not properly enclosed.
Corrected code:
//Execute "while" loop
while (x <= 100)
{
//Perform calculation
total += x;
//Increment "...
Explanation of Solution
Given code:
//Execute "while" loop
while (y > 0)
{a
//Print the value of "y"
System.out.println(y);
//Increment "y" by "1"
++y;
Error:
- In the given code, the body of “while” is not properly enclosed.
- The letter “a” given after opening brace “{” is not related to any syntax and it leads to an error.
Corrected code:
//Execute "while" loop
while (y > 0)
{
//Print the value of "y"
System.out...
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
Java How To Program (Early Objects) (10th Edition)
- Consider the following pseudo code, Method func() { PRINT “This is recursive function" func() } Method main( { func() } What will happen when the above snippet is executed?arrow_forward#python def add_func(a,b): print(f"add_func output for 1 + 2: {add_func(1, 2)}") print(f"add_func output for good + day: {add_func('good',' day')}") (add_func(1,2) = 3, (add_func('good',' day') = "good day"arrow_forward1、Counting Primitive Operations void STRAITMAXMIN(A,n,max,min) //Set the maximum value in A to max and the minimum value to min { int i,n max=min=A[1] for i=2 to n { if A[i]> max max=A[i]; if A[i]< min min=A[i]; } }arrow_forward
- int f(int &k){k++;return k * 2;}int main(){int i = 1, j = -1;int a, b, c;a = f(i) + i/2;b = j + f(j) + f(j);c = 2 * f(j);return 0;} What are the values of a, b and c id the operands in the expressions are evaluated from left to right and then what are the values when its evaluated right to left?arrow_forwardExercise 1: (Design of algorithm to find greatest common divisor) In mathematics, the greatest common divisor (gcd) of two or more integers is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4. Why? Divisors of 8 are 1, 2, 4, 8. Divisors of 12 are 1, 2, 4, 6, 12 Thus, the common divisors of 8 and 12 are 1, 2, 4. Out of these common divisors, the greatest one is 4. Therefore, the greatest common divisor (gcd) of 8 and 12 is 4. Write a programming code for a function FindGCD(m,n) that find the greatest common divisor. You can use any language of Java/C++/Python/Octave. Find GCD Algorithm: Step 1 Make an array to store common divisors of two integers m, n. Step 2 Check all the integers from 1 to minimun(m,n) whether they divide both m, n. If yes, add it to the array. Step 3 Return the maximum number in the array.arrow_forwardQ1:Find the output of the following code:arrow_forward
- JAVA CODE ONLY AND PROVIDE OUTPUT SCREENSHOT PLEASEarrow_forwardEnglish Language Calculator Build a simple “English Language” calculator that does the following: Takes three inputs from the keyboard Two of the inputs are single-digit numbers (0 to 9) The third input is a char from the keyboard, representing one of the five operations from the keyboard: + (addition) - (subtraction) * (multiplication) / (division) ^ (exponentiation) Output the description of the operation in plain English, as well as the numeric result Input and Output Instruction for the Calculator If the two input numbers are 5 and 3, and the operation is *, then the output should be five multiplied by three is 15 Note that the result is given as a number, not a word. If the two numbers are 2 and 9, and the operation is -, then the output should be two minus nine is -7 If the two numbers are 5 and 2, and the operation is ^, then the output should be five to the power two is 25 Hint: To perform the exponentiation, use the pow method of the Math class. If the two…arrow_forwardc# console programingarrow_forward
- When you perform arithmetic operations with operands of different types, such as adding an int and a float, ____________. C# chooses a unifying type for the result you must choose a unifying type for the result you must provide a cast you receive an error messagearrow_forward(Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forward(Electrical eng.) a. An engineer has constructed a two-dimensional array of real numbers with three rows and five columns. This array currently contains test voltages of an amplifier. Write a C++ program that interactively inputs 15 array values, and then determines the total number of voltages in these ranges: less than 60, greater than or equal to 60 and less than 70, greater than or equal to 70 and less than 80, greater than or equal to 80 and less than 90, and greater than or equal to 90. b. Entering 15 voltages each time the program written for Exercise 7a runs is cumbersome. What method could be used for initializing the array during the testing phase? c. How might the program you wrote for Exercise 7a be modified to include the case of no voltage being present? That is, what voltage could be used to indicate an invalid voltage, and how would your program have to be modified to exclude counting such a voltage?arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning