Freelancer

Area of a rectangle

Area of a rectangle - C Programming Example

The following description is an example C program code of determining the area of a rectangle.
C Programming Example
Area of a rectangle

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    int l,w,area;
    printf("input the length \n");
    scanf("%d",&l);
    printf("input the width \n");
    scanf("%d", &w);
    area=l*w;
    printf("the area of rectangle is= %d", area);
    getch();
}

Output of the code

Area of a rectangle - C programming examples
Area of a rectangle - C programming examples

Explanation

#include<stdio.h>
#include<conio.h>
·         This are called header file. A header file is a file with extension .h which contains c function declarations and macro definitions to be shared between several source files. The c programming language provides many standard library functions for file input and output. These functions make up the bulk of the c standard library header <stdio.h>. On the other hand <conio.h>is a c header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing “console input and output” form a program.
Main()
·         In C programming, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to the function. The function returns control to main when a return statement is executed or when end of function is reached.
Int l,w,area;
·         Int is a variable or data type. A variable declared to be of type int (integer) can be used for storing integer numbers. Here we declare “sum”, “a”,”b” and “i” variable in int type.
Printf() function
·         C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h .Anu text written within the pair of quotes (“”) is displayed as such by printf() function on the screen.
Scanf() function
·         This function is used to get input from the user of the program. Scanf(“%d”,&a) is used here. So, this program will read in an integer value of “a” valuable that the user enters on the keyboard (%d is for the integer values. As there is “&a”, the number user enters will be the value of “a” valuable.
Printf("the area of rectangle is= %d", area);
·         The calculated area of the rectangle is outputted by this line. The “%d” represents the area of the rectangle.
Getch()
·         Getch() is added in order to stop the program. It is used in order to stop the program while the header file <conio.h> is used. 

Final result

This program will show us the the area of a rectangle which is calculated by the inputted length and width of the rectangle. 

No comments

Powered by Blogger.