C++/C++

Advertisement


Question
I need to find all the numbers that are perfect square between 1000 and 9999 and the first 2 digits are the same and the last 2 digits are the same>

Answer
to find all numbers between 1000 and 9999 which are perfect squares,

a. determine the integer a which is just larger than square root of 1000
b. determine the integer b which is just larger than square root of 9999
write a loop to get the square of each integer in the interval [a,b).

note: use the function std::sqrt() in header <cmath> to determine the square root of a floating point number.

#include <cmath>

int main()
{
 int from = int( std::sqrt( 1000.0 ) ) + 1 ;
 int to = int( std::sqrt( 9999.0 ) ) + 1 ;

 for( int n = from ; n < to ; ++n )
 {
   int square = n * n ;
   // ....
 }
}


the first two digits of a positive four digit number x is the quotient you get when you divide x by 100.

the last two digits of a positive number x is the remainder you get when you divide x by 100.

       if( ( x / 100 ) == ( x % 100 ) )
the first two digits of x are the same as the last two digits.

combine these and you will have your program.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.