- 4

Can any one help me with this code in C

perimeter and area of a field are given as 100m and 225sq m then find Length and Breadth. L>B

27th Jul 2021, 9:36 AM
Joe
7 Answers
+ 6
Did you try on your own, did you try to find the formula needed from the web or anything? Posting a working code directly won't benefit you.
27th Jul 2021, 9:42 AM
minirkk
minirkk - avatar
+ 3
You can derive length and breadth from the perimeter and area, since perimeter is equal to twice the length plus twice the breadth, while area is the product of length and breadth. However, seeing that you need a code to do this, the expected method to solve this here is most likely to just iterate through possible values of L and B while checking whether these values satisfy the given conditions: L+B == 50 L*B == 225
27th Jul 2021, 10:35 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
If you know about quadratic formula it would be much easier and direct - w * l = 225 (area of rectangle) 2(w + l) = 100 (perimeter of rectangle) => w + l = 50 w * l = 225 - - - - - (1) w + l = 50 - - - - - (2) from (1), we have l = 225/w replacing l in (2), we get w + 225/w = 50 wÂČ - 50w + 225 = 0 using quadratic formula, w = [- (-50) ± sqrt((-50)ÂČ - 4*1*225)] / 2*1 w = [50 ± sqrt(2500 - 900)] / 2 w = (50 ± 40)/2 Now, either w = (50 + 40)/2 = 45 or w = (50 - 40)/2 = 5 So, either l=5, w=45 or l=45, w=5 But since it's given that l>w l = 45, w = 5 is the answer. How to convert this into codes? #include <stdio.h> int main() { int a=225,p=100; for(int l=1;l<a;l++) { for(int b=1;b<=l;b++){ if(l*b==a && l+b==p/2){ printf("length=%d\n", l); printf("breath=%d", b); break; } } } return 0; }
27th Jul 2021, 10:55 AM
minirkk
minirkk - avatar
+ 2
@Joe then show your attempt.
27th Jul 2021, 10:33 AM
The future is now thanks to science
The future is now thanks to science - avatar
0
int main() { int a=225,p=100,l,b; p == 2*(l+b); a == l*b; printf("length=%d\n", l); printf("breath=%d", b); return 0; }
27th Jul 2021, 10:52 AM
Joe
0
Joe programming in C cannot solve algebraic equations by logical operators. #include <stdio.h> #include <math.h> int main() { int a = 225, p = 100, l, b; l = (int)(p + 2*sqrt((p/2)*(p/2) - 4*a))/4; b = p/2 - l; printf("length = %d\n", l); printf("breath = %d\n", b); }
27th Jul 2021, 11:56 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
- 1
I did try, but really can't understand.
27th Jul 2021, 9:58 AM
Joe