What is the bug in the following code? What do you call this kind of loop bug?
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Absolute Java (6th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
Problem Solving with C++ (10th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
- Write code that uses any type of loop. The code should continually ask for a user input and sums all user inputs that are divisible by 2. The loop should continue until the user enters a negative number. The code should output the sum a single time once user entry has completed. You may assume all libraries and namespaces have been previously written into the code, you are just writing everything that would go inside the main function (beyond the return 0:).arrow_forwardPlease unroll the following loop three times. What is the benefit of loop unrolling? for (i=0; i<42; i++) { a[i] = b[i] * i;}arrow_forward#include | int main() { int n=5; for (int i=1;iarrow_forwardCan you always convert a while loop into a for loop? Convert the following while loop into a for loop. int i = 1;int sum = 0;while (sum < 10000) {sum = sum + i; i++;}arrow_forwardplease correct this code #include <stdio.h> int fact(int n) { // as n! is undefined for n<0 if(n<0)return 0; // so just return 0 // by default factorial will be 1(means if n=0 then factorial is 1) int fact = 1; // for n > 0, calculate the factorial using for loop for(int i = 1; i <= n; i++) fact *= i; return fact; // then return the calculated factorial }arrow_forwardJava program Part B: For Loop Program Write a program to compute the sum of the first N Fibonacci numbers, where N is an integer that is input by the user. Once it computes the sum, the program will prompt the user for a new value of N. The program will exit if the user enters a non-integer number or string (such as “quit”) instead of an integer. For both the above problems, the first four numbers of the Fibonacci sequence are 0, 1, 1, and 2. Part B must use at least one For loop, but may also use while loops. Invalid (negative) numbers should be flagged and handled. Output should be like : Welcome to the Fibonacci AdderTo quit, end a non-integer number or string How many Fibonacci numbers should be summed? >> 10The sum of the first 10 Fibonacci numbers is: 88 How many Fibonacci numbers should be summed? >> xxx Thank You for Using the Fibonacci Adderarrow_forwardPrimeAA.java Write a program that will tell a user if their number is prime or not. Your code will need to run in a loop (possibly many loops) so that the user can continue to check numbers. A prime is a number that is only divisible by itself and the number 1. This means your code should loop through each value between 1 and the number entered to see if it’s a divisor. If you only check for a small handful of numbers (such as 2, 3, and 5), you will lose most of the credit for this project. Include a try/catch to catch input mismatches and include a custom exception to catch negative values. If the user enters 0, the program should end. Not only will you tell the user if their number is prime or not, you must also print the divisors to the screen (if they exist) on the same line as shown below AND give a count of how many divisors there are. See examples below. Your program should run the test case exactly as it appears below, and should work on any other case in general. Output…arrow_forwardWATERSORT PROGRAM - JAVA •The aim of Part 2 is to set up the puzzle for the player to solve in Part 3. IMPORTANT NOTE: We are not going to start with 2 empty bottles, but rather with 8 empty slots spread over the 5 bottles as explained in the intro video. Start with 5 empty bottles. Use a random number generator to fill bottles one slot at a time with the colours while keeping 8 slots empty – there is a total of 5*4 = 20 slots – so if we will 12 slots, 8 are free. •Advantage: Easy to create a puzzle with a good mix •Disadvantage: With more colours and bottles and only 2 open bottles for more advanced versions of the game, the result might not be solvable. Start with three sorted bottles. In the strategy the idea is to load three bottles with uniform colour and then move ink around for a number of moves until the bottles are mixed up. •Advantage: Result is always solvable since the bottles are created in a reversed-game strategy. •Disadvantage: It is hard to develop an algorithm which…arrow_forwardHow many times will the following loop execute? int numl H int num2 do B 5; -100; num1-5; while (num1 - 5);arrow_forwardresult = 0 for n in range(6): result = n - 3 if (result % 2) != 0: print('-', end=' ') continue print(result, end=' ') print('done') What is the output of this codearrow_forwardOct 13 8:38 Aarrow_forward#include <stdio.h> int main(){ int a; /* for loop execution */ for ( a = 1; a <= 100; a++){ printf("%d\n",a * a); } return 0; } write a code using while loop that will provide the same output as the code given above.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education