C++ Language | Solving for Cube Root

Wednesday 2 January 2013


In mathematics specially in equations in algebra, there are times that square roots and cube roots appear. Square root is the process of multiplying a number by itself. While, cube root is the process of multiplying a number by itself three times to get an answer. Sometimes people tend to confuse these two equations because they sound almost the same.

Thinking about this, I come up with an idea to make a program that can compute for the cube root of a number. This program is used with a float data identifier to allow users enter a number with or without decimal points. Check out the codes below.


   1:  #include <iostream>
   2:  #include <conio.h>
   3:   
   4:  using namespace std;
   5:   
   6:  int main()
   7:  {
   8:      float side = 0.0f;
   9:      float sideCubed = 0.0f;
  10:      cout << "Enter a real number: ";
  11:      cin >> side;
  12:      sideCubed = side*side*side;
  13:      cout << side << "^3 = " << sideCubed << endl;
  14:      getch();
  15:  }
  16:   
  17:


The program runs like this. It will ask the user to enter a number to be computed. Then it will be stored in a memory named side. Your input will then be computed, multiplied to itself three times, then the result will be stored in the memory named sideCubed. SideCubed will be the one to be called under the cout statement to show the final result. Then you can terminate the program afterwards. Check out the sample output below.




Hope you learned something from this post. Feel free to leave a comment below. Thanks!

No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...