Ask Question

What will be the result of running the following code fragment? int year = 0; double rate = 5; double principal = 10000; double interest = 0; while (year < 10) { interest = (principal * year * rate) / 100; System. out. println ("Interest " + interest); }

+2
Answers (1)
  1. 4 June, 18:47
    0
    This code fragment will run an infinite loop

    Explanation:

    This output: Interest 0.0 Will be displayed infinitely. The reason is because the variable year which is initially set to 0 is never updated and as such remains true because the condition is while (year<10). So at the first iteration the statement interest = (principal * year * rate) / 100; evaluates to 0 and this line of code System. out. println ("Interest " + interest); prints Interest 0.0. At the next iteration the same evaluation and output takes place and on and on and on ... since the control variable is not changing.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What will be the result of running the following code fragment? int year = 0; double rate = 5; double principal = 10000; double interest = ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers