C Language | Surface Area and Volume of a Cube

Friday 7 December 2012

When we play board games there will always be a dice that holds a series of numbers that represents how many our moves must be in the game. A dice is shaped like a cube, it has six equal sides. Cube shaped things are seen everywhere. I was always amazed by the cube because of its special form and unique shape, because of this I always wonder how to calculate its area and volume considering how big it can be.

As I was going through my High School and College years I learned how to calculate the area and volume of a cube and so much more things. To share and help you with calculating the area and volume of a cube I made a program that allows a user to calculate the said areas easily. Check out the sample code below.

     1:  
   2:  #include <conio.h>
   3:  #include <stdio.h>
   4:  #include <math.h>
   5:   
   6:  float sol1(float y, float n1)
   7:  { 
   8:          return y = n1 * n1 * n1;
   9:         
  10:          
  11:        }
  12:  float sol2(float z, float n1)
  13:  { 
  14:          
  15:         return z = (n1 * n1) * 6;
  16:          
  17:        }     
  18:   
  19:  int main()
  20:  {
  21:      float z = 0, y = 0,n; 
  22:      int counts,x;
  23:      
  24:      printf("Enter how many counts: ");
  25:      scanf("%d", &counts); 
  26:     
  27:      if(counts>100){
  28:           printf("You have exceeded the maximum input of 100");
  29:           getch();
  30:           return 0;
  31:           }
  32:      
  33:      for (x = 1; x <= counts; x++ ) {
  34:          printf("\nEnter Numbers: ");
  35:          scanf("%f", &n);
  36:          if(n>5000){
  37:          printf("You have exceeded the maximum input of 5000");
  38:           getch();
  39:           return 0;
  40:           }
  41:         
  42:          printf("Cube #%d - %.2f(SA) and %.2f(V)\n", x, sol2(z,n), sol1(x,n));
  43:          }
  44:          
  45:  getch();
  46:  }


We know that to get the area of a cube we need to find first the area of one side and that would be the length squared then it will be multiplied six times to get the whole area value of the cube. Furthermore, to get the volume of a cube we need to measure it in cubic units. To solve for the volume you only need to cubed the length of one side since its size is the same with the other sides one side will be enough.

As seen above, there are two data types present, the int and the float. The int data type identifier holds the counts on how many inputs a user want to enter and how many times the loop must repeat the statements. While the float data type identifier holds the equations on getting the area and volume of the cube. When the program is run, it will ask the user to input how many times he want to enter a number. Then, The program will now execute the equations within the float functions. After that, main function will now call on the results from the float functions and print it out in the screen. After the program has finished you can then terminate it. You can check out the sample output below.




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

1 comment:

  1. I remember learning programming and creating such programs in Turbo Pascal. Today, I often use ready-made solutions such as services - platform enablement because I know that they significantly simplify everyday work. It's great that we currently have access to such solutions.

    ReplyDelete

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...