Write a loop in C++ to printout the following sequence of numbers: 4 12 20 28 36 44 52
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.
Write a loop in C++ to printout the following sequence of numbers: 4 12 20 28 36 44 52
Here is the approach to solve the given program
- In the c++ everything starts with the main method . Hence we have to create the main method first .
- In the main method create the variable to hold the values which we are gonna print such as the 4 12 etc.
- Initialize the variable with the value 4 first .
- Print the number sequence text and the 4 number first .
- Then create the loop . The loop can be created using the for statement in the c++ programming language .
- We want to generate total 6 more number as we have already take the 4 .
- We can see that the number have the difference of 8 among them hence we add the value of 8 into the number in every loop and print the number to the console .
Check the code and output in the second step . Everything is mentioned in the comments .
Step by step
Solved in 2 steps with 2 images