CODE SNIPPET :

#include<stdio.h>

int main(){
    int l,b,a;
    b=15,l=20;
    printf("**********************************\n");
    printf("CALCULATION OF AREA OF A RECTANGLE\n");
    printf("**********************************\n");
    printf("Length = %d\n",l);
    printf("Breadth = %d\n",b);
    a=l*b;
    printf("\nArea of the rectangle = %d\n",a);
    printf("**********************************\n");
    return 0;
}

 


Hello guys,
today we are going to discuss about another basic code snippet of C language. 
YES!! Definitely its one level up than the "Hello World" code. In this code, we are going to learn about the logic implementation using C language. 

NOTE : The given code might not work on old compilers. But, you can check it using any online compiler for your own convenience.

So where were we?
Yes, logic implementation! How do we actually going to do it by the way!
Don't worry, here I am, to tell you the whole story behind the code. The most basic thing about this particular code is the formula of the area of a rectangle. If you are not aware of the formula, I am going to tell you in below.
ABCD - rectangle



This is our given rectangle and we have to calculate it's area. 
For ABCD rectangle, It's dimensions are, 

AB = CD = 15 (breadth)
BC = DA = 20 (length)

Now, to calculate the area of ABCD, we have to use this formula : 

area = (length * breadth)

Now we can derive the actual area of ABCD, that is (15*20) = 300


CODE ANALYSIS:

So, this is just simple mathematics. Now it's time to use it in our code. But how can it be possible?
Let's understand the above-given snippet code line by line:

  1. #include<stdio.h>: At first, we should include this code to call our standard input output directory for an easy run of our code.
  2. int main(){…}: This will be included in our program because we need a function where we can work our things out. That function is called main() function and it will return an integer type value that is int in short.
  3. int l,b,a;: Now, we are in the main() function. So, our first work is to declare some basic variables that are necessary for our program. Here we have taken three integer type variables those are l, b and a. (Do not forget to include ';' after each specific line, that I included)
  4. b=15,l=20;: You can easily understand this notation, but for those who can't, I am here for you guys. This line tells us, we are giving two values to our integer type variable b and l, respectively 15 and 20. Now, our b variable holds 15 and our l variable holds 20, those are our rectangle's breadth and length respectively. 
  5. printf("******************\n");: Don't you dare to be confused after seeing these many stars in this line. It's just for some designation. You have to only focus on the printf() function, which is here to print whatever we want the program to print for us. We must put everything; we need to print in between double inverted commas in this function. And if you do not know about \n (backslash n), this character is used to print a new line, like the  (ENTER) button, we use while typing. All other lines containing printf() function have the same usage.
  6. printf("Length = %d\n",l);: As you can see here, I have taken a specific line although it is a printf() function. Let's see, why this line is so special. We can see some %d notation here, this thing usually helps us to print some integer value, that is contained by a defined variable, like l in this line. We have to include the variable name after the closing double inverted comma, with a comma (,) before it. The same thing happened in some of our next lines.
  7. a=l*b;: This step is the most important step in our program. You have already learned about the mathematical formula for the rectangle area above this post. Now, when it comes to our C language, the most beautiful thing is we can just order our compiler to do some mathematics like addition, subtraction, multiplication, division etc. and it will return us the result accurately. By this line, we ordered our compiler to multiply l with b and put it in the variable a. That's the whole meaning of this line. And after that, we just have to print the value of a variable to display the area of our rectangle.
  8. return 0;: Another important line is that return part. It means our program that is written in our main() function has finished its work and now it is returning an integer value as expected, that is 0. This 0 is also a symbol of success. If the main function is returning 0, it means the program should be executed without any error. But, if it returns 1, then it is confirmed that the program execution is not a success, it has some error while running. 
So that is the End of our analysis part. Now let's understand the output of our program. You can also check it by pasting the above code in any online compiler. Here is our output:


CODE OUTPUT:

**********************************

CALCULATION OF AREA OF A RECTANGLE

**********************************

Length = 20

Breadth = 15

 

Area of the rectangle = 300

********************************** 
 

>> Download the .c file (coming soon)
>> Download the .exe file (coming soon)