Next Perfect Squares for a Given Number

In summary, the code demonstrates using event-controlled loops to create a sequence for determining the next largest and next smallest perfect squares. To determine the next largest perfect square, the code uses a while loop to increment the square root until it is larger than the given number, and then outputs the square of the incremented root. For the next smallest perfect square, the code uses a do-while loop to increment the square root until it is larger than 1000, and then outputs the square of the decremented root. To create a custom square root function, a similar approach could be used, but the code would need to account for the given decimal points.
  • #1
mr.me
49
0
Heres my problem, step one I've solved. Step two only ever returns the original newRoot*newRoo and I don't understand how to correct itTo demonstrate event controlled loops use arithmetic to create:
1.Create a sequence to determine the next largest perfect square
2.Create another sequence to determine the next smallest perfect square for a value less than 1000

My code:

Code:
#include <iostream>
#include <math.h>
#include <iomanip>          
using std::cout;
using std::cin;
using std::endl;

main()
{
   cout << "Enter a number for which to find the next largest perfect square: ";
   int number;
   cin >> number;
   int sqRoot = 1;            
   while (sqRoot*sqRoot < number)  // sqRoot is too small
      sqRoot++;                 //  try the next number
   cout << sqRoot*sqRoot << endl;
   
    cout<< "Enter a number for which to find the next smallest perfect square: ";
    int newNumber;
    cin>>newNumber;
    int newRoot =1000;
    while (newRoot*newRoot < 1000 )
        newRoot-+newRoot;
    cout<< newRoot*newRoot<<endl;
  
}
 
Last edited:
Physics news on Phys.org
  • #2
I think it is because your while loop is never actually done because 1000*1000>1000

not sure what your trying to do here
newRoot-+newRoot
 
  • #3
I guess that was an obvious mistake :-p

Still I am not sure how to construct my loop for the problem in question

With the second loop I wanted to check each iteration until a got a value less-than the user entered value...

So if I entered 6 I wanted it to return a 4 because that is the first perfect square less than 6

Im not sure how to do this on paper or with the while loop.
 
  • #4
hmm... I think you might to try a do while loop and increase the number by 1 each time and check again
 
  • #5
x=1
y=0
while(y<input)
{
if(x*x>input)
break
y=x*x;
x++
}

cout <<y

I think that might do ityou could say "using namespace std" instead of saying all the using at the top
 
Last edited:
  • #6
Thank-you, I see what I was doing wrong.

We are required to use each std:: for our work, instead of loading the whole namespace.

Thanks again :smile:
 
  • #7
As an aside, i.e. not homework, what if I wanted to write a square root function, that we allow you to set the given decimal points for stuff like the square of 7 or the square of 10.?

I know C++ already has a sqrt function but could I use my code, clumsy though it may be or would I have to do something entirely different?
 

Related to Next Perfect Squares for a Given Number

1. What is a loop in C++?

A loop in C++ is a programming construct that allows a set of instructions to be repeated multiple times until a certain condition is met. It helps to avoid writing repetitive code and makes programs more efficient and concise.

2. How do you declare and initialize a loop in C++?

To declare and initialize a loop in C++, you first need to define a loop variable and initialize it with a starting value. Then, you can use a loop control statement such as "for", "while", or "do-while" to set the condition for the loop to run and specify the actions to be performed inside the loop.

3. What is a perfect square in C++?

In C++, a perfect square is a number that can be obtained by multiplying an integer by itself. For example, 9 is a perfect square because it is the result of 3 x 3. In other words, a perfect square is a number whose square root is a whole number.

4. How do you check if a number is a perfect square in C++?

To check if a number is a perfect square in C++, you can use the sqrt() function from the library. This function returns the square root of a given number. If the square root is a whole number, then the number is a perfect square. Otherwise, it is not a perfect square.

5. Can you give an example of using a loop to find all perfect squares between 1 and 100 in C++?

Yes, here is an example code using a "for" loop to find all perfect squares between 1 and 100 in C++:

for(int i = 1; i <= 100; i++) {
    int squareRoot = sqrt(i);
    if(squareRoot * squareRoot == i) {
        cout << i << " is a perfect square." << endl;
    }
}

This code iterates through all numbers from 1 to 100 and checks if their square root is a whole number. If it is, then the number is a perfect square and it is printed to the console.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
790
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top