0

I'm new to programming and my teacher doesn't really teach us things. I need to make a program that finds the first 20 perfect squares using the pow function. NEEDS TO BE A COUNTER CONTROLLED LOOP. I really don't know what that means but I gave it my best shot below. Please help me!

#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

int main ()
{
    int a, b;
    cout << "Enter the num:";
    scanf("% d", &a);
    b = sqrt (a);
    if ((b * b) == a)
        cout << "The given number is a perfect square";
    else
        cout << "The given number is not a perfect square";
    getch();
}
3
  • This looks like it would have been a solution to a previous task. Have you attempted the loop task yet? Dec 30, 2013 at 17:54
  • 1
    You are first including a C++ Header, then you are including a C Header and then again you are including a C++ Header. I hope this code does not come from your teacher.
    – VoidStar
    Dec 30, 2013 at 17:56
  • 1
    Have you looked at your text book to figure out what a "counter controlled" loop is? Asked your teacher? Another student?
    – Eric
    Jan 8, 2014 at 3:05

1 Answer 1

1

If I understand correctly then counter controlled loop means that it should be a loop that has a variable(the counter) and a condition(for the loop), and while the variable answers the condition it should loop. In your example an integer i will be the counter and the loop will be a for loop:

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    for(int i = 1; i < 21; i++)
    {
       cout << i << "'s perfect square is " << pow(i, 2) << endl;
    }
}
2
  • Thank you so much for your help! My teacher basically want us to teach ourselves because "it is the best way to learn" Dec 30, 2013 at 18:03
  • 1
    No problem, you welcome. If this answers your question consider accepting this answer.
    – MoonBun
    Dec 30, 2013 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.