Problem Analysis
Total space occupied by a flat surface on a particular shape is called the area. Area is the count of the number of squares that can fit in that shape. Thus the number of unit squares that can fit into a rectangle gives its area.
Area of rectangle by length and width:
A rectangle is defined by its length and width.
Area of the rectangle is – (length × width) square units.
Thus the area of the rectangle is equal to length times its width.
Area of rectangle by Diagonal
The diagonal is the straight line connecting opposite vertices. Since a rectangle has four vertices, a rectangle can have two diagonals. Area of a rectangle in terms of diagonals can be defined as:
width(√ ( (diagonal)2 – (width)2 ))
Problem Description
In C language, program to calculate area of a rectangle can be developed in 2 ways:
- Using length and breadth of rectangle
- Using diagonal and width of rectangle
To develop a program to calculate area using length and breadth of the rectangle, length and breadth must be known. Length and breadth can be in decimal point so its data-type must be float. The variable that stores value of area should also be in float data type.
Program to calculate area of rectangle using diagonal can be developed using two ways:
- Using in-built functions
- Using user-defined functions
Program to calculate area using diagonal requires calculation of square difference of diagonal and width and then finding its square root. C language has an inbuilt function for calculating the square of a number and square root of a number. Function for square and square root is defined in the library “math.h”. To use the function square and square root “math.h” has to be included in the program.
Program to calculate the area of the rectangle using diagonal can also be developed without using inbuilt fumctions. When inbuilt functions are not used then the function to calculate the square and square root has to be defined explicitly.
Solution to Problem
Following are 3 program to find the area of rectangle:
- Using length and breadth of the rectangle
- Using diagonal of the rectangle (having inbuilt functions)
- Using diagonal of the rectangle (having user defined function)
Area of a rectangle program in C (length and breadth given)
#include <stdio.h> int main() { float l,b,a; printf("Enter length of area "); scanf("%f", &l); printf("\n"); printf("Enter width of rectangle "); scanf("%f", &b); a = l * b; printf("\n"); printf("Area of rectangle is %f ", a); return 0; }
Output: Enter length of area 2 Enter width of rectangle 3 Area of rectangle is 6.000000
Code Analysis In the above code three user defined float type variables are declared - l, b, a. User input is taken in variable l (length of rectangle) and b (width of rectangle). Programming instruction to do this is: printf("Enter length of area "); scanf("%f", &l); printf("Enter width of rectangle "); scanf("%f", &b); Area of the rectangle is calculated using length and breadth. Programming instruction to do this is: a = l * b; Area is stored in user defined float variable a. This is done by following programming instructions: printf("Area of rectangle is %f ", a);
Area of a rectangle program in C (using diagonal and inbuilt function)
#include<stdio.h> #include<math.h> void main() { float d,w,a; printf("Enter width of the rectangle\n"); scanf("%f",&w); printf("\n"); printf("Enter diagpnal of the rectangle\n"); scanf("%f",&d); printf("\n"); a = (w * sqrt( pow(d,2) - pow(w,2)) ); printf("Area of rectangle is = %f", a); }
Output Enter width of the rectangle 2 Enter diagonal of the rectangle 3 Area of rectangle is = 4.472136
Code Analysis This program finds the area of the rectangle when the diagonal and width of the rectangle is known. Calculating the area using diagonal and width requires calculation of square root and square of a number. To find square root and square of a number header-file math.h is included. #include User input for diagonal and width is taken in user-defined float variables w and d. Programming instruction for this is: printf("Enter width of the rectangle\n"); scanf("%f",&w); printf("Enter diagonal of the rectangle\n"); scanf("%f",&d); Area of the rectangle is calculated by executing following programming instruction: a = (w * sqrt( pow(d,2) - pow(w,2)) ); Result is stored in variable ‘a’ and displayed on the output window, by executing following programming instruction: printf("Area of rectangle is = %f", a);
Area of a rectangle program in C (using diagonal and user-defined function)
#include<stdio.h> #include<math.h> float srt(float n); float pw(float p); void main( ) { float d,w,a,n,p_d,p_w,d_w,s_q; printf("Enter width of the rectangle\n"); scanf("%f",&w); printf("\n"); printf("Enter diagonal of the rectangle\n"); scanf("%f",&d); printf("\n"); p_d = pw(d); p_w = pw(w); d_w = p_d - p_w; s_q = srt(d_w); a = w * s_q; printf("Area of rectangle is = %f", a); } float srt (float n) { double s_tart,e_nd,m_id; s_tart = 0, e_nd = n; while((e_nd - s_tart)>=0.000001) { m_id = (s_tart + e_nd)/2; if(m_id * m_id < n) s_tart = m_id; if(m_id * m_id >= n) e_nd = m_id; } return m_id; } float pw(float p) { long long p_ower = 1; int i; for(i=1; i<=2; i++) { p_ower = p_ower * p; } return p_ower; }
Output: Enter width of the rectangle 2 Enter diagonal of the rectangle 3 Area of rectangle is = 4.472136
Code Analysis This program calculates the area of the rectangle by user-defined functions to calculate square root and square of a number. This program has two user-defined functions - float srt(float n); float pw(float p); Inside function main ( ), user input for width and diagonal is taken in variables w and d. Square of w and d is calculated by the user-defined function pw(float n). Inside function pw(float n) square of the number is calculated using for loop. for loop variable i is initialized to1 and terminating condition of for loop is i <= 2. In condition i <= 2, 2 is used since the square of a number is calculated. Inside for loop variable p_ower is multiplied with variable p. This is done by executing following programming instructions: for(i=1; i<=2; i++) { p_ower = p_ower * p; } If the value of p is 3: i p_ower = p_ower * p 1 p_ower = 1 * 3 P_ower = 3 2 p_ower = 3 * 3 p_ower = 9 When square is calculated then square-root is calculated by calling the user-defined function float srt (float n). Square root of the number is calculated using the Divide-and-Conquer. Inside the function float srt (float n) two user-defined variables s_tart and e_nd are taken. s_trat is set 0 and end set to n. This is done by following instruction: s_tart = 0, e_nd = n; Square root is calculated inside a while loop. Inside the while loop value of variable s_tart and e_nd is added and divided by 2 and result is stored in m_id. m_id = ( s_tart + e_nd ) / 2; The value of m_id is multiplied by itself and then compared with the value n if it is smaller than n then s_tart is set to m_id. if(m_id * m_id = n) e_nd = m_id;
Code AnalysisThis program calculates the area of the rectangle by user-defined functions to calculate square root and square of a number. This program has two user-defined functions – float srt(float n); float pw(float p); Inside function main ( ), user input for width and diagonal is taken in variables w and d. Square of w and d is calculated by the user-defined function pw(float n). Inside function pw(float n) square of the number is calculated using for loop. for loop variable i is initialized to1 and terminating condition of for loop is i <= 2. In condition i <= 2, 2 is used since the square of a number is calculated. Inside for loop variable p_ower is multiplied with variable p. This is done by executing following programming instructions: for(i=1; i<=2; i++) { p_ower = p_ower * p; } If the value of p is 3:
When square is calculated then square-root is calculated by calling the user-defined function float srt (float n). Square root of the number is calculated using the Divide-and-Conquer. Inside the function float srt (float n) two user-defined variables s_tart and e_nd are taken. s_trat is set 0 and end set to n. This is done by following instruction: s_tart = 0, e_nd = n; Square root is calculated inside a while loop. Inside the while loop value of variable s_tart and e_nd is added and divided by 2 and result is stored in m_id. m_id = ( s_tart + e_nd ) / 2; The value of m_id is multiplied by itself and then compared with the value n if it is smaller than n then s_tart is set to m_id. if(m_id * m_id < n) s_tart = m_id; If the value of m_id is greater than m_id the e_nd is set to m_id. if(m_id * m_id >= n) e_nd = m_id; Loop execution cycle is like this:
When the loop condition becomes false we get the square root of the number and the value is returned to the main ( ) function. Inside the main ( ) function this value is multiplied with width to obtain the area of the rectangle.
|
Conclusion
Area of rectangle program in C calculates area of rectangle when length and width of rectangle is known. Formula to calculate area is:
Area = Length × Width
If Diagonal and width of the rectangle is known than area is calculated using formula:
width(√ ( (diagonal)2 – (width)2 ))
Area of the rectangle is a simple program but its understanding is critical since it is an application of – Divide-and-Conquer.
Leave a Reply