- 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
7 ответов
+ 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.
+ 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
+ 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;
}
+ 2
@Joe then show your attempt.
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;
}
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);
}
- 1
I did try, but really can't understand.