Assume has been included and the following variables have been declared in main(): Create code that would be a part of main() that do the following: Use a while loop to prompt the user to enter how many numbers will be entered and store this value in the howmany variable. Stay in the loop as long as the user enters a value that is less than or equal to zero. When the while loop is finished, use a for loop to do the following: Prompt the user to enter a number. You will need to declare a loop counter variable and it must be used in this prompt. Store this number in the num variable. If the number is negative, add 1 to the total variable. The howmany variable must be used in the condition for this loop. When the while loop is finished, display the number of negative numbers entered in a sentence.
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
Assume <iostream> has been included and the following variables have been declared in main():
Create code that would be a part of main() that do the following:
- Use a while loop to prompt the user to enter how many numbers will be entered and store this value in the howmany variable. Stay in the loop as long as the user enters a value that is less than or equal to zero.
- When the while loop is finished, use a for loop to do the following:
- Prompt the user to enter a number. You will need to declare a loop counter variable and it must be used in this prompt.
- Store this number in the num variable.
- If the number is negative, add 1 to the total variable.
- The howmany variable must be used in the condition for this loop.
- When the while loop is finished, display the number of negative numbers entered in a sentence.
Here is a sample output:
Step by step
Solved in 3 steps with 1 images