Program to
find area and perimeter of rectangle
Let’s see how to write this C program
#include<stdio.h>
#include<conio.h>
int main()
{
int length,width;
int perimeter,area;
printf("enter
length and width of rectangle \n");
scanf("%d
%d",&length,&width);
perimeter=2*(length+width);
area=length*width;
printf("\n
perimeter is = %d \n area is = %d",perimeter,area);
getch();
return 0;
}
Explanation of the program
int length,width;
int perimeter,area;
here we are declaring four variables as
length to store length of a rectangle.
width to store width of a rectangle.
perimeter to store perimeter after calculating it.
area to store area after calculating it.
scanf("%d %d",&length,&width);
use to store the user desired values in the variables of
length and width.
perimeter=2*(length+width);
basic mathematical expression to calculate perimeter of the
rectangle it is equivlant to { preimeter=2(length+width) }.
area=length*width;
basic mathematical expression to calculate perimeter of the
rectangle it is equivlant to { area=length
x width }.
printf("\n perimeter is = %d \n area is =
%d",perimeter,area);
this statement will going to print “perimeter is = “
perimeter_value then change line and
print “area is = “ area_value
%d
This tells the compiler that we are going to display or read
the integer number.
\n or endl
This signifies the endline or we can say change line.
In this program it is responsible to change the line after
printing the perimeter of rectangle.
Getch(); and return 0;
We already discussed this click here to move back.
Pictures of program:
Step 1:
Create the program:-
Step 2: output
Note: if any of the programmer is facing the
problem of reappering of the previous screen again and again
He can use the function
clrscr();
It present in the
conio.h header file
After declaring the
variables
No comments:
Post a Comment