Skip to content
The Programming School

The Programming School

  • Home
  • About
  • Contact Us
  • Toggle search form

Loops examples in C++ Language

Here are some Loops examples in C++ Language

Working with For Loops

Counting from 1 to 5:

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 5; i++) {

cout<< i << " ";

}

cout<< endl;

return 0;

}

• The ' for ' loop starts with ' i ' set to 1.

• It continues as long as ' i ' is less than or equal to 5.

• In each iteration, it prints the value of ' i ' and increments it.

Printing Even numbers up to 10:

#include <iostream>

using namespace std;

int main() {

for (int i = 2; i <=10; i+=2) {

cout<< i << " ";

}

cout<< endl;

return 0;

}

• The for loop starts with ' i ' set to 2.

• It continues as long as ' i ' is less than or equal to 10.

• In each iteration, it prints the value of ' i ' (even numbers) and increments ' i ' by 2.

Counting Down from 5 to 1:

#include <iostream>

using namespace std;

int main() {

for(int i = 5; i >= 1; i--) {

cout<< i << " ";

}

cout<< endl;

return 0;

}

• The ' for ' loop starts with ' i ' set to 5.

• In continues as long as ' i ' is greater than or equal to 1.

• In each iteration, it prints the value of ' i ' and decrements it.

Working with While Loops

User Input Validation:

#include <iostream>

using namespace std;

int main() {

int age;

cout<< "Enter your age (between 18 and 99) : ";

cin>> age;

while (age < 18 || age > 99) {

cout<< "Ivalid age. Please enter a valid age between 18 and 99 : ";

cin>> age;

}

cout<< "Valid age entered : " << age << endl;

return 0;

}

• The ' while ' loop ensure that the user enters a valid age between 18 and 99.

• If the entered age is invalid, it prompts the user to enter a valid age.

Power of 2 Series:

#include <iostream>

using namespace std;

int main() {

int power = 1;

int exponent = 0;

cout<<" Power of 2 series up to 8 : ";

while (exponent <= 8) {

cout<<power << " " ;

power *= 2;

++exponent;

}

cout<<endl;

return 0;

}

• The ' while ' loop generates a series of power of 2 up to 2^8.

• It starts with ' power ' set to 1 and ' exponent ' set to 0.

• In each iteration, it prints the current value of ' power ' , multiplies ' power ' by 2, and increments the ' exponent '.

• The loop continues until the ' exponent ' reaches 8.

Guess the Number Game:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

srand(time(0));

int secretNumber = rand() % 100 + 1;

int guess;

int attempts = 0;

cout<< "Welcome to the Guess the Number Game!" <<endl;

while (true) {

cout<< "Enter your guess (between 1 and 100) : ";

cin>> guess;

++attempts;

if (guess == secretNumber) {

cout<< "Congratulations! You guessed the number in " <<attempts << " attempts. " <<endl;

break;

} else if (guess < secretNumber) {

cout<< " Too low. Try again." <<endl;

} else {

cout<< " Too high. Try again." <<endl;

}

}

return 0;

}

• The ' while ' loop continues until the user correctly guesses the random ' secretNumber ' .

• It generates a random number between 1 and 100 using ' rand() % 100 + i ' .

• The user is prompted to enter a guess, and the program provides feedback (too low, too high) based on the comparison with secret number.

• The loop exits when the user correctly guesses the number, and the total number of attempts is displayed.

Working with Do While Loops

Even Number Up to 10:

#include <iostream>

using namespace std;

int main() {

int evenNumber = 2;

do {

cout<< evenNumber << " ";

evenNumber += 2;

} while (evenNumber <= 10);

cout<< endl;

return 0;

}

• The ' do-while ' loop prints numbers from 1 to 5.

• It starts with ' number ' set to 1 .

• Inside the loop , it prints the current value of ' number ' and increments it .

• The loop continues until ' number ' is less than or equal to 5 .

Factorial Calculation:

#include <iostream>

using namespace std;

int main() {

int n, factorial = 1;

cout<< " Enter a positive integer : " ;

cin>> n;

if (n < 0) {

cout<< " Please enter a non-negative integer. " << endl;

return 1;

}

do {

factorial *= n;

--n;

} while (n > 0);

cout<< " Factorial: " << factorial << endl;

return 0;

}

• The program calculates the factorial of a given positive intefer ('n') .

• The ' do-while ' loop continues as long as ' n ' is greater than 0 .

• In each iteration , it multiplies ' factorial ' by ' n ' and then decrements ' n ' .

• The loop repeats until ' n ' becomes 0 , resulting in the calculation of the factorial .

Guess the number Game (with Replay Option):

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

srand(time(0));

char playAgain;

do {

int secretNumber = rand() % 100 + 1;

int guess;

int attempts = 0;

cout<< " Welcome to the Guess the Number Game! " << endl;

do {

cout<< " Enter your guess (between 1 and 100) : ";

cin>> guess;

++attempts;

if(guess == secretNumber) {

cout<< " Congratulations! You guessed the number in " << attempts << " attempts. " <<endl;

} else if (guess < secretNumber) {

cout<< " Too low. Try again. " <<endl;

} else {

cout<< " Too high. Try again. " <<endl;

}

} while (guess != secretNumber);

cout<< " Do you want to play again> (y/n) : ";

cin>> playAgain;

} while (playAgain == 'y');

cout<< " Thanks for playing! " <<endl;

return 0;

}

• The outer ' do-while ' loop allows the player to replay the game.

• Inside the outer loop, there is an inner ' do-while ' loop for the number guessing game.

• The player is prompted to enter guesses until they correctly guess the randomly generated ' secretNumber ' .

• After each game, the player is asked if they want to play again. If ' y ' is entered, the game restarts. Otherwise, the program exits.

  • Home
  • About
  • Contact Us

Copyright © 2025 The Programming School.

Powered by PressBook Masonry Dark