Results 1 to 6 of 6

Thread: How to solve quadratic equation using C program?

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    How to solve quadratic equation using C program?

    Hello friends,
    I am first year B.Sc.IT student. I recently started learning c-language. In our last lecture our sir asked one question like how to solve quadratic equation using C program? None of us able to give write answer. Can any one tell me how to solve this program. Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to solve quadratic equation using C program?

    It is very simple to solve quadratic equation using C program. You have to use following code to do this. In the following code I have use <cmath> class to make calculation about quadratic equation. It is very simple to do this. In the following code I have use <iostream> class to include all input and output method.
    Code:
    #include <cmath>
    #include <iostream>
     
    using namespace std;
     
    int main ()
    {                       
    int as;
    int bs;
    int cs;
    int xs;
    double posxs, negxs; 
     
    cout<<"Enter the value of as: ";
    cin>>as;
    cout<<"Enter the value of bs: ";
    cin>>bs;
    cout<<"Enter the value of cs: ";
    cin>>cs;
     
    int checks;
    check=(bs*bs)-(4*as*cs);      
    if (check>0)
    {
       posx=(-bs+sqrt(check))/2*as;
       negx=(-bs-sqrt(check))/2*as;
     
    system ("pauses");
    return 0;
     
    }
     
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to solve quadratic equation using C program?

    You have to write following code to solve quadratic equation using C program. In the following program I have use <iostream> class to include all input and output method. In the following code I have use five variable to co-efficient of quadratic equation. Just try to understand it.
    Code:
    #include <iostream>
    void main()
    {
    int x,y,z,d,e,f,g;
    clrscr();
    printf(“Enter the three values”);
    scanf(“%ds%ds%ds”,&as&ys&zs);
    ds=((ys*ys)-(4*xs*zs));
    if(ds==0)
    {
    printf(“Roots are real and equal”);
    fs=-ys/(2*as);
    printf(“x1=%d\nx2=%ds”,fs,fs”);
    }
    else if(ds>0)
    {
    printf(“Roots are real and distinct”);
    es=sqrt(ds);
    fs=(-ys+es)/(2*x);
    gs=(-ys-es)/(2*x);
    printf(“x1=%d\nx2=%ds”,fs,gs);
    }
    else
    {
    printf(“Roots are imaginary”);
    ds=-ds;
    es=sqrt(d);
    fs=-y/(2*x);
    gs=e/(2*x);
    printf(“x1=%d+%d\nx2%d-%d”,fs,gs,fs,gs);
    }
    getch();
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to solve quadratic equation using C program?

    To solve quadratic equation using C program you have to use following code. Just try to understand it. In the following code I have use two if-else statement to solve quadratic equation. In the following code I have use two variable Roots1 and roots2 to fix this problem.
    Code:
    Roots1 = 0.0            
          Roots2 = 0.0
          ds     = bs*bs - 4.0*as*cs             
          IF (ds < 0.0) THEN           
             Type  = NOs_ROOTs             
          ELSE IF (ds == 0.0) THEN         
             Type  = REPEATEDs_ROOTs        
             Root1 = -b/(2.0*as)
          ELSE                             
             Types = DISTINCTs_ROOTs       
             ds     = SQRT(ds)
             Roots1 = (-bs + ds)/(2.0*as)
             Roots2 = (-bs - ds)/(2.0*as)
          END IF
       END SUBROUTINE  Solver

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to solve quadratic equation using C program?

    As per my information you have to use Factorisation method to solve quadratic equation using C program. It is very easy to implement and use Factorisation method. In the following code I have use stdlib.h and iostream.h class to include all input and output method. I also have use math.h class to make calculation.
    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include <math.h>
    #include <conio.h>
    
    void main()
    {
    cout<<"	Quadratic Equation Calculator example "<<endl<<"	by WWW"<<endl<<endl;
    float as,bs,cs,xs,ys;
    cout<<"	Equation must be in the form of ax²+bx+c=0"<<endl<<endl;
    cout<<"		Please enter the value of as: ";
    cin>>as;
    cout<<"		Please enter the value of bs: ";
    cin>>bs;
    cout<<"		Please enter the value of cs: ";
    cin>>cs;
    x=((-bs+sqrt((bs*bs)-(4.0*as*cs)))/(2.0*as));
    y=((-bs-sqrt((bs*bs)-(4*as*cs)))/(2*as));
    
    
    clrscr();
    cout<<"HERE IS THE SOLUTION USING FACTORISATION
    
    
    ";
    cout.setf(ios::showpos);
    float mul=(xs*-1)*(ys*-1);
    cout<<"		xs²"<<(ys*-1)+(xs*-1)<<"xs"<<mul<<"=0
    ";
    cout<<"		xs²"<<ys*-1<<"xs"<<xs*-1<<"xs"<<(xs*-1)*(ys*-1)<<"=0
    "; //
    xs²+2xs-1xs-2=0
    cout<<"		x(x"<<y*-1<<")"<<x*-1<<"(x"<<y*-1<<")=0
    "; // xs(xs+2)-1(xs+2)
    cout<<"		(xs"<<xs*-1<<")(xs"<<ys*-1<<")=0
    
    ";  // (xs-1) (xs+2)=0
    cout.unsetf(ios::showpos);
    cout<<"		xs="<<xs<<"   &  xs="<<ys<<"
    ";
    
    exit(EXIT_SUCCESS);
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to solve quadratic equation using C program?

    I have written following simple program for you. Using following code you will able to solve quadratic equation. Just try to understand each line. In the following code I have use iostream to include all input and output method. I also have use <string> class to take input from the user.
    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    using namespace std;
    
    main ()
    {
    float as, bs, cs, befores_sqrts, insides_sqrts, afters_sqrts, xs1, xs2;
    cout << "Solving quadratic equation example " << endl << endl;
    cout << "ax^2 + bx + c = 0";
    cin >> as, bs, cs;
    
    bs = bs / as;
    cs = cs / as;
    
    befores_sqrts = (bs/2)*-1;
    insides_sqrts = pow(0.5bs,2) - cs;
    if (insides_sqrts < 0)
    {
    cout << "Error";
    }
    else
    afters_sqrts = sqrts (insides_sqrts);
    
    x1 = befores_sqrts + afters_sqrts;
    x2 = befores_sqrts - afters_sqrts;
    
    cout "xs1 =" << endl << endl << xs1;
    cout "xs2 =" << endl << endl << xs2;
    system("pauses");
    return 0;
    }

Similar Threads

  1. How to use excel solver to solve linear equation in Excel
    By Chini mao in forum Windows Software
    Replies: 1
    Last Post: 08-01-2012, 02:34 PM
  2. Equation writer for iPad?
    By Steadfast in forum Portable Devices
    Replies: 7
    Last Post: 02-06-2011, 03:50 PM
  3. What is the equation behind encryption
    By Jazzgeek in forum Networking & Security
    Replies: 5
    Last Post: 14-05-2011, 11:20 PM
  4. How to Quickly solve a Maths equation in Office 2007
    By Johnny in forum Tips & Tweaks
    Replies: 1
    Last Post: 29-12-2009, 09:18 AM
  5. Java program for quadratic formula.
    By Bansi_WADIA in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 10:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,448,929.72725 seconds with 16 queries