In: Computer Science
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values =============================================
2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers // declare variable integer and relate that to input scanner // declare variable sum equals to zero // declare while loop condition where the variable you just created above is not equal to 0 // inside of the bracket of while condition sum the data with addition // prompt the user to enter integer unless its not zero // again the same variable that you created outside the while condition will ask to input value through scanner // outside the bracket display the sum of values
3) Declare the do while loop // declare the variable integer which is equal to zero // declare do-while loop and inside of brackets prompt the user to print word "Hello" // increment the variable integer that you declared outside the loop // outside brackets declare while condition where variable that you declared in the beginning is less then 25
4) Continue the same process as exercise number 3, but this time use for loop
5) Create break command in while loop // declare integer variables sum and number and both of them are equal to zero // create while condition where number is less then 25 // inside of brackets increment number // declare if condition where number is equal to 10 // write continue ++ // sum the number (addition) // outside of brackets display sum in console
6) In this exercise do the same thing like number 5 exercise but instead of continue ++ write break;
We have declared int sum = 0, Then with the help of while loop, we are going on adding the value of i from 0 to 24, using the condition while(i<25).
The code with output is:
Again, in the second question, we have to ask user to input numbers, and add them, until a zero is pressed. This is checked by a sentinel value in the while condition.
The code and its output is:
In the third question, we have to use a do while loop to input "hello" 25 times. The code and output is:
In the fourth question, we have do the same as third question, but with a for loop