pranjal1805

Function-3

May 15th, 2021
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. float l,m;
  4. void find_roots(float f[]);
  5. void find_sum_product(float f[]);
  6. int main()
  7. {  
  8.     printf("Enter coefficients a,b and c:\n");
  9.     int x;
  10.     float rootsarray[3];
  11.     for(int i=0;i<3;i++)
  12.     {
  13.         scanf(" %e", &rootsarray[i]);
  14.     }
  15.     printf("1.Only Roots.\n");
  16.     printf("2.Roots and operations.\n");
  17.     printf("Choice:\n");
  18.     scanf(" %d", &x);
  19.     switch(x)
  20.     {
  21.         case 1:
  22.         {
  23.             find_roots(rootsarray);
  24.         }
  25.  
  26.         case 2:
  27.         {
  28.             find_sum_product(rootsarray);
  29.         }
  30.  
  31.     }
  32.     return 0;
  33. }
  34.  
  35. void find_roots(float f[])
  36. {  
  37.     float z=((f[1]*f[1])-4*(f[0]*f[2]));
  38.     if(z>0)
  39.     {
  40.       l=((-1*f[1])+ pow(z,0.5))/(4*f[0]*f[2]);
  41.       m=((-1*f[1])+ pow(z,0.5))/(4*f[0]*f[2]);
  42.       printf("Roots are:%.2f and %.2f", l, m);
  43.     }
  44.     else if(z==0)
  45.     {
  46.         l=(-1*f[1])/(4*f[0]*f[2]);
  47.         printf("Both roots are equal and are:%.2f", l);
  48.     }
  49.     else
  50.     {
  51.         printf("Roots are imaginary");
  52.     }
  53. }
  54.  
  55. void find_sum_product(float f[])
  56. {  
  57.     float sum=0,product=0;
  58.     float z=((f[1]*f[1])-4*(f[0]*f[2]));
  59.     if(z>0)
  60.     {
  61.       l=((-1*f[1])+ pow(z,0.5))/(2*f[0]);
  62.       m=((-1*f[1])- pow(z,0.5))/(2*f[0]);
  63.       printf("Roots are:%.2f and %.2f\n", l, m);
  64.     }
  65.     else if(z==0)
  66.     {
  67.         l=(-1*f[1])/(2*f[0]);
  68.         printf("Both roots are equal and are:%.2f\n", l);
  69.     }
  70.     else
  71.     {
  72.         printf("Roots are imaginary\n");
  73.     }
  74.     printf("\n");
  75.     printf("Sum before is %f\n", sum);
  76.     printf("Product before is %f\n", product);
  77.     printf("\n");
  78.     sum=(-1*f[1])/(f[0]);
  79.     product=(f[2])/(f[0]);
  80.     printf("Sum after is %f\n", sum);
  81.     printf("Product after is %f\n", product);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment