Counter Controlled While Loop Using Perfect Squares
Categories:
Counter-Controlled While Loop for Perfect Squares in C++
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.
i * i
), be mindful of potential integer overflow if the limit is very large. For extremely large numbers, consider using long long
for i
and limit
to prevent unexpected behavior.How the Code Works
- Include Header:
#include <iostream>
is used for input/output operations. - User Input: The program prompts the user to enter an
upper limit
. - Initialization: An integer variable
i
is initialized to1
. Thisi
acts as our counter, representing the base number whose square we are checking. - Loop Condition: The
while (i * i <= limit)
condition is evaluated before each iteration. The loop continues as long as the square ofi
does not exceed thelimit
. - Print Square: Inside the loop,
i * i
(the current perfect square) is printed. - Increment Counter:
i++
increments the counteri
by 1. This is crucial for the loop to eventually terminate and to move to the next potential perfect square. - Termination: When
i * i
becomes greater thanlimit
, the loop condition becomes false, and the program exits the loop, printing a newline character and then terminating.