Find the percentage change in the area of a Rectangle
Given two integers P and Q which represents the percentage change in the length and breadth of the rectangle, the task is to print the percentage change in the area of the rectangle. Examples:
Input: P = 10, Q = 20 Output: 32 Explanation: Let the initial length of the rectangle be 100 and breadth be 80. Initial area = 8000. New length = 110 and new breadth = 96. Therefore, the new area = 10560. The percentage change in the area = ((10560 – 8000) / 8000) * 100 = 32. Input: P = 20, Q = -10 Output: 8 Let initial length of the rectangle be 100 and breadth be 80. Initial area = 8000. New length = 120 and new breadth = 72. Therefore, new area = 8640. The percentage change in the area = ((8640 – 8000) / 8000) * 100 = 8.
Approach:
area = length * breadth
- Let the initial length of the rectangle be L and the breadth of the rectangle be B. Therefore, the initial area is given by L * B.
- Therefore, the new length and breadth are given as:
L' = L + ((P/100)*L)
B' = B + ((Q/100)*B)
- Therefore, the new length and breadth are given as:
new area = [L + ((C/100)*L)] * [B + ( ( D / 100) * B)]
- The percentage change in the area is given by the formula:
% change = ((new area – old area) / old area )*100
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculate_change( int length, int breadth){
int change = 0;
change = length + breadth+((length * breadth)/100);
return change;
}
int main()
{
int cL = 20;
int cB = -10;
int cA = calculate_change(cL, cB);
printf ( "%d" ,cA);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int calculate_change( int length, int breadth){
int change = 0 ;
change = length + breadth+((length * breadth)/ 100 );
return change;
}
public static void main(String args[])
{
int cL = 20 ;
int cB = - 10 ;
int cA = calculate_change(cL, cB);
System.out.println(+ cA);
}
}
|
Python3
def calculate_change(length, breadth):
change = 0
change = length + breadth + ((length * breadth) / / 100 )
return change
if __name__ = = "__main__" :
cL = 20
cB = - 10
cA = calculate_change(cL, cB)
print (cA)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int calculate_change( int length, int breadth){
int change = 0;
change = length + breadth + ((length * breadth)/100);
return change;
}
public static void Main(String[] args)
{
int cL = 20;
int cB = -10;
int cA = calculate_change(cL, cB);
Console.Write(cA);
}
}
|
Javascript
function calculate_change( length, breadth){
let change = 0;
change = length + breadth+ Math.floor((length * breadth)/100);
return change;
}
let cL = 20;
let cB = -10;
let cA = calculate_change(cL, cB);
console.log(cA)
|
Time Complexity: O(1)
Auxiliary Space: O(1) as it is using constant space for variables
Last Updated :
18 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...