C++ Program to Print All Perfect Cubes below 1000

«
»
This C++ program prints all the perfect cubes below 1000. The program first calculates the value below which all the numbers have their cubes less than 1000 and then prints all their cubes.

Here is the source code of the C++ program which prints all the perfect cubes below 1000. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Print All Perfect Cubes Below 1000
  3.  */
  4. #include <iostream>
  5. #include <cmath>
  6. #include <iomanip>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int max = 1000, lim;
  12.  
  13.     lim = cbrt(max);
  14.     cout << "Perfect cubes below 1000" << endl;
  15.     for(int i = 1; i < lim; i++)
  16.     {
  17.         cout << i << " ^ 3  : " << setw(3) << setfill('0')
  18.              << pow(i, 3) << endl;
  19.     }
  20. }

$ a.out
Perfect cubes below 1000 
1 ^ 3  : 001
2 ^ 3  : 008
3 ^ 3  : 027
4 ^ 3  : 064
5 ^ 3  : 125
6 ^ 3  : 216
7 ^ 3  : 343
8 ^ 3  : 512
9 ^ 3  : 729

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.