Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- float l,m;
- void find_roots(float f[]);
- void find_sum_product(float f[]);
- int main()
- {
- printf("Enter coefficients a,b and c:\n");
- int x;
- float rootsarray[3];
- for(int i=0;i<3;i++)
- {
- scanf(" %e", &rootsarray[i]);
- }
- printf("1.Only Roots.\n");
- printf("2.Roots and operations.\n");
- printf("Choice:\n");
- scanf(" %d", &x);
- switch(x)
- {
- case 1:
- {
- find_roots(rootsarray);
- }
- case 2:
- {
- find_sum_product(rootsarray);
- }
- }
- return 0;
- }
- void find_roots(float f[])
- {
- float z=((f[1]*f[1])-4*(f[0]*f[2]));
- if(z>0)
- {
- l=((-1*f[1])+ pow(z,0.5))/(4*f[0]*f[2]);
- m=((-1*f[1])+ pow(z,0.5))/(4*f[0]*f[2]);
- printf("Roots are:%.2f and %.2f", l, m);
- }
- else if(z==0)
- {
- l=(-1*f[1])/(4*f[0]*f[2]);
- printf("Both roots are equal and are:%.2f", l);
- }
- else
- {
- printf("Roots are imaginary");
- }
- }
- void find_sum_product(float f[])
- {
- float sum=0,product=0;
- float z=((f[1]*f[1])-4*(f[0]*f[2]));
- if(z>0)
- {
- l=((-1*f[1])+ pow(z,0.5))/(2*f[0]);
- m=((-1*f[1])- pow(z,0.5))/(2*f[0]);
- printf("Roots are:%.2f and %.2f\n", l, m);
- }
- else if(z==0)
- {
- l=(-1*f[1])/(2*f[0]);
- printf("Both roots are equal and are:%.2f\n", l);
- }
- else
- {
- printf("Roots are imaginary\n");
- }
- printf("\n");
- printf("Sum before is %f\n", sum);
- printf("Product before is %f\n", product);
- printf("\n");
- sum=(-1*f[1])/(f[0]);
- product=(f[2])/(f[0]);
- printf("Sum after is %f\n", sum);
- printf("Product after is %f\n", product);
- }
Advertisement
Add Comment
Please, Sign In to add comment