Counter Controlled While Loop Using Perfect Squares

Learn counter controlled while loop using perfect squares with practical examples, diagrams, and best practices. Covers c++, while-loop development techniques with visual explanations.

Counter-Controlled While Loop for Perfect Squares in C++

Illustration of a while loop iterating through numbers, with perfect squares highlighted.

Learn how to implement a counter-controlled while loop in C++ to identify and print perfect squares up to a specified limit.

The while loop is a fundamental control flow statement in C++ (and many other programming languages) that allows a block of code to be executed repeatedly as long as a specified condition remains true. A common pattern is the 'counter-controlled' while loop, where a variable acts as a counter, incrementing or decrementing with each iteration until a termination condition is met. This article will guide you through using a counter-controlled while loop to find and display perfect squares up to a user-defined limit.

Understanding Perfect Squares

A perfect square is an integer that can be expressed as the product of an integer with itself. For example, 9 is a perfect square because it is 3 * 3. Similarly, 16 is a perfect square (4 * 4), and 25 is a perfect square (5 * 5). In our program, we will iterate through numbers, calculate their squares, and print them if they fall within the specified limit.

flowchart TD
    A[Start Program] --> B{Initialize counter 'i' to 1}
    B --> C{Get 'limit' from user}
    C --> D{Is i*i <= limit?}
    D -- Yes --> E[Calculate square = i*i]
    E --> F[Print square]
    F --> G[Increment 'i']
    G --> D
    D -- No --> H[End Program]

Flowchart of the counter-controlled while loop logic for perfect squares.

Implementing the Counter-Controlled While Loop

To implement this, we'll need a counter variable, typically named i or count, initialized to 1. The while loop's condition will check if the square of our counter (i * i) is less than or equal to the user-defined limit. Inside the loop, we'll print the perfect square and then increment our counter. This ensures that the loop eventually terminates when the square of i exceeds the limit.

#include <iostream>

int main() {
    int limit;
    std::cout << "Enter an upper limit for perfect squares: ";
    std::cin >> limit;

    int i = 1; // Initialize counter
    std::cout << "Perfect squares up to " << limit << ":\n";

    // Counter-controlled while loop
    while (i * i <= limit) {
        std::cout << i * i << " ";
        i++; // Increment counter
    }
    std::cout << std::endl;

    return 0;
}

C++ code for finding perfect squares using a counter-controlled while loop.

How the Code Works

  1. Include Header: #include <iostream> is used for input/output operations.
  2. User Input: The program prompts the user to enter an upper limit.
  3. Initialization: An integer variable i is initialized to 1. This i acts as our counter, representing the base number whose square we are checking.
  4. Loop Condition: The while (i * i <= limit) condition is evaluated before each iteration. The loop continues as long as the square of i does not exceed the limit.
  5. Print Square: Inside the loop, i * i (the current perfect square) is printed.
  6. Increment Counter: i++ increments the counter i by 1. This is crucial for the loop to eventually terminate and to move to the next potential perfect square.
  7. Termination: When i * i becomes greater than limit, the loop condition becomes false, and the program exits the loop, printing a newline character and then terminating.