Showing posts with label for loop. Show all posts
Showing posts with label for loop. Show all posts

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:  { 

C Language | For Loop Statement

Wednesday, 5 December 2012


After I showed you how the Do-While Loop executes, I will now show you the second looping statement which is more easier to use, the For Loop. For Loop repetition statement is a one line argument. It holds the control variable, condition and the increment of control variable in one line/argument, which makes it more easy. For Loop is used on most programs because it helps a lot in making a program short and straight.

Now, I made a sample program to show you how the for loop works. The sample I made is the same example I had in the do-while loop. I just changed it to for loop statement. In this example, you will observe the difference between the do-while and for loop statements and how the codes are arranged. Check out the codes below.

     1:  
   2:  #include <stdio.h>
   3:  #include <conio.h>
   4:   
   5:  int main()
   6:  {
   7:  int n;
   8:  int value = 1;

C Language | Addition part 2


Last time, I showed you how to create a simple addition program using C language. Now, we will still be adding numbers but I put something new in it. Yes, we will still add integers/numbers but it is now in the users decision how many integers he will add.

We will still use int as our data identifier because we are still inputting integers. The new command we're going to put into the code is the for loop statement. This statement will control how many inputs a user want. From the name itself, loop, it means infinity. For loop is a repetitive statement, the program will not stop/end until the argument inside the loop is met. This statement is useful when you have to make repetitive programs because this will excuse you from doing the same code again and again.

The program you see below is an example of adding numbers but inputs can be manipulated by the user. Check out the codes below.


   1:   
   2:   
   3:   
   4:  #include <stdio.h>
   5:  #include <conio.h>
   6:   
   7:  int main()
   8:  {
   9:      int a, b = 0, c = 1, sum = 0;
  10:   
  11:      printf("How many numbers you want to add?: ");
  12:      scanf("%d", &a);
Related Posts Plugin for WordPress, Blogger...